Computer Science, asked by namazzisalmah2486, 7 hours ago

Write a C++ program that using nested for loop to print the following pattern. 1010101
 10101 
  101    
 1

Answers

Answered by anindyaadhikari13
2

\texttt{\textsf{\large{\underline{Solution}:}}}

Correct pattern is -

1 0 1 0 1 0 1  

 1 0 1 0 1  

    1 0 1  

       1

\rule{300}{2}

The co‎‎de -

#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