Write a C++ program that using nested for loop to print the following pattern. 1010101
10101
101
1
Answers
Answered by
2
Correct pattern is -
1 0 1 0 1 0 1
1 0 1 0 1
1 0 1
1
The code -
#include <iostream>
using namespace std;
int main(){
int i,j,spaces=0;
for(i=7;i>=1;i-=2,spaces+=2){
for(j=1;j<=spaces;j++)
cout<<" ";
for(j=1;j<=i;j++)
cout<<j%2<<" ";
cout<<endl;
}
return 0;
}
Note: If you want to remove the spaces between 1 and 0, just write cout<<j%2 and in place of "spaces+=2", write "spaces++".
See the attachment for output.
Attachments:
Similar questions