Piramide Model

Piramide Model:

Output:

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


code:

#include<stdio.h>
int main()
{
    int row,start=1,digit,i,j;
    scanf("%d",&row);
    for(i=1;i<=row;i++)
    {
        for(j=1;j<=(row-i)*2;j++)
            printf(" ");
        for(j=1,digit=start;j<=i;j++,digit++)
            printf("%d ",digit%10);
        digit-=2;
        while(digit>=start)
            printf("%d ",digit--%10);
        printf("\n");
        start++;
    }
}





Discussion:
On the piramide look at all the first digit.
            1
         2 3 2
        3 4 5 4 3
      4 5 6 7 6 5 4
    5 6 7 8 9 8 7 6 5
  6 7 8 9 0 1 0 9 8 7 6
7 8 9 0 1 2 3 2 1 0 9 8 7

 All the row start by maintain a rule. first 1,second 2,third 3...…………….nth n.

And also notice that it increase one by one and pint number of line time.
            1 line 1 print 1 time.
          2 3 line 2 print 2 time.
        3 4 5 line 3 print 3 time.
      ……………….
     …………………
n n-1 n-2 ……… line n print n time.

Then it decrease through first digit of the row.
             digit=0 where start=1  

2            digit=2 where start=2

4 3          digit=4 where start=3 

6 5 4        digit=6 where start=4
8 7 6 5      digit=8 where start=5

0 9 8 7 6    digit=10 where start=6

so we get a variable "start", for store first value of row.
Then the value of first "start" take into "digit" variable.
It print and increase number of line time.
Ending this loop the value of "digit" make printed value+1.
So We have to get printed value-one. For this reason assign
        digit=digit-2
And print digit While digit>=start.
After completing a row get a new line and increase the of start.

মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

Big Big mod

শিরোনামহীন গল্প

Dictionaries and Maps