Saturday, January 11, 2014

Printing Pattern 6

1
3 2
6 5 4
10 9 8 7

Outer Loop : 4 Times

Inner Loop: depend on value of Outer Loop

Value in each line is maximum that line should print in reverse order

1
2 3
4 5 6
7 8 9 10

If each line is above is reversed we get desired pattern

We must start with a maximum value of the line and then it should then be decremented.
What is the maximum value for the line?
Looking at the pattern --
Maximum value        line no.             Value of i + previous Max value
1                                    1                              1+ 0 = 1
3                                    2                              1+ 2 =3
6                                    3                              3+ 3 =6
10                                  4                              4+ 6 =10

#include<stdio.h>
int main()
{
    int i,j,max_val,print_val;

    max_val=0;
    for(i=1;i<=4;i++)
    {
        max_val=max_val+i;
        print_val=max_val;
        for(j=1;j<=i;j++)
        {
            printf("%d ",print_val);
            print_val--;
        }
        printf("\n");
    }
    return 0;
}


No comments:

Post a Comment