+ 1
Help me to design a pattern using c or c++
1 6 2 10 7 3 13 11 8 4 15 14 12 9 5
3 Antworten
+ 3
Look at this code.
https://code.sololearn.com/cnxHT5M7EEEp/#cpp
+ 1
Can you please give further explanation?
+ 1
#include<iostream>
using namespace std;
int getn(int fn)
{
int ctr=0;
for(int i=1;fn!=1;i++) { ctr++; fn-=i; }
return ctr;
}
int main()
{
int n=5, high;
// cin>>n;
// Case 1, from number of rows.
// cin>>high; n=getn(high);
// Case 2, from highest number.
int val=1,val2,step,fstep=n;
for(int i=0;i<n;i++)
{
step = n-i; val2=val;
for(int j=0;j<=i;j++)
{
cout<<val2<<" ";
val2-=step; step++;
}
cout<<endl;
val += fstep; fstep--;
}
}