Pages

Sunday, April 6, 2014

C code to print right triangle shape using nested for loops

Concept used: nested for loop
Post Contains: 


  • C++ code 
  • Line by line variable values and output
  • Image view of code
  • Image view of sample output of program

    C++ Code:

#include<iostream>
using namespace std;
int main()
{
   for(int i=0;i<=5;i++){

        for(int j=0;j<=i;j++)
            {
                cout<<j;
            }
       cout<<endl;
    }
 return 0;
}



Line by line variable values and output

i=0 
j=0
output
         0


i=1
j=0 to j=1
output 
          0
          0  1

i=2
j=0 to j=2
output 

          0
          0  1
          0  1  2 

i=3
j=0 to j=3
output


          0
          0  1
          0  1  2 
          0  1  2  3
   
i=4
j=0 to j=4

output 


          0
          0  1
          0  1  2 
          0  1  2  3
          0  1  2  3  4
   

i=5
j=0 to j=5

final output: 


          0
          0  1
          0  1  2
          0  1  2  3
          0  1  2  3  4
          0  1  2  3  4  5



Image view of program:
shape code to print right triangle using for loop
c++ shape code to print right triangle using for loop

Sample output image view: 
sample output cplusplus shape code to print right triangle using for loop
sample output shape code to print right triangle using for loop

Related Posts by Categories

0 comments:

Post a Comment