<>

Write a program, which generates and prints a triangle of numbers.


Write a program, which generates and prints a triangle of numbers. The triangle should be generated using two integer inputs, one is for the size of triangle and other is for the start value of triangle. Size gives the number of columns. Start value specifies the first value of the first column. Column n contains n values. The successive values are obtained by adding 1 to the previous value. When 9 is reached, the next value becomes 1. 
Note: Your program should print the numbers in the form of triangle exactly as shown below. The space between two numbers is optional. 

Expected outputs:

Size 6, start 1

1 2 3 4 5 6
  7 8 9 1 2
    3 4 5 6
      7 8 9
        1 2
          3 

Size 5, start 3

  3 4 5 6 7
    8 9 1 2
      3 4 5 
        6 7  
          8  


#include <iostream.h>
void main()
{
     int  size,x,y,z=1;

     cout<<"Enter Size: ";
     cin>>size;

     cout<<"Enter Value: ";
     cin>>z;

     for(x=size; x>0; x--)
     {
          for(y=0; y<size-x; y++)
              cout<<"  ";
             
          for(y=1;y<=x;y++)
          {
              cout<<z<<" ";
              z=z+1;
             
              if(z%10==0)
              z=1;
          }
    
          cout<<endl;
   }

}

Rate this posting:
{[['']]}

0 comments:

Post a Comment

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

 
TOP