12345
23451
34512
45123
51234
print the pattern in qbasic
Answers
Answered by
1
Explanation:
I did this pattern by constructing an n digit number (the number is 12345 for n=5) and then according to the row number, I simply shifted the digits of this number.
There really is no need to use nested for loops to do this problem. All you need are 2 mutually independent for loops.
Here’s my C++ code…
#include <iostream>
using namespace std;
int main()
{
int n,num=0,k=1;
cout<<"Enter the Number of Rows : ";
cin>>n;
for(int i=1;i<=n;i++)
{
num=num+i;
num=num*10;
k*=10;
}
num=num/10;
k=k/10;
for(int i=1;i<=n;i++)
{
cout<<num<<endl;
num=(num%k)*10+num/k;
}
return 0;
}
Here are a few Test Cases I tried out…
Attachments:
Similar questions