Pattern type -1

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

For given size of N print the following pattern

EXPLANATION:

we have given n and we have to print pattern according to it
suppose n=4 is given
then pattern looks like

1
2*3
4*5*6
7*8*9*10

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n,i,j,c=1;
cin>>n;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
if(j<i){
cout<<to_string(c) +“*”;
c++;
}
else{
cout<<to_string(c);
c++;
}
}cout<<“\n”;
}
//cout<<c;
c=c-n;

// for(i=n;i>=1;i–){
// for(j=1;j<=i;j++){
// if(j<i){
// cout<<to_string(c) +“*”;
// c++;

// }
// else{
// cout<<to_string(c);
// c++;
// }
// }
// c= (c+1)-2*i;
// cout<<“\n”;

// }
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n,i,j,c=1;
cin>>n;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
if(j<i){
cout<<to_string(c) +“*”;
c++;
}
else{
cout<<to_string(c);
c++;
}
}cout<<“\n”;
}
//cout<<c;
c=c-n;

// for(i=n;i>=1;i–){
// for(j=1;j<=i;j++){
// if(j<i){
// cout<<to_string(c) +“*”;
// c++;

// }
// else{
// cout<<to_string(c);
// c++;
// }
// }
// c= (c+1)-2*i;
// cout<<“\n”;

// }
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n,i,j,c=1;
cin>>n;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
if(j<i){
cout<<to_string(c) +“*”;
c++;
}
else{
cout<<to_string(c);
c++;
}
}cout<<“\n”;
}
//cout<<c;
c=c-n;

// for(i=n;i>=1;i–){
// for(j=1;j<=i;j++){
// if(j<i){
// cout<<to_string(c) +“*”;
// c++;

// }
// else{
// cout<<to_string(c);
// c++;
// }
// }
// c= (c+1)-2*i;
// cout<<“\n”;

// }
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}