Friday, November 23, 2012

WRITE A CPROGRAM TO GENERATE THE FOLLOWING OUT PUT:


/* WRITE A CPROGRAM TO GENERATE THE FOLLOWING OUT PUT:

            b)                     1
                                 1 2 1
                              1 2 3 2 1
                           1 2 3 4 3 2 1
                        1 2 3 4 5 4 3 2 1
*/

#include<stdio.h>
#include<conio.h>

#define MAXLINE 5

void main()
{
            int curlin,i;
            clrscr();

            curlin=1;
            while(curlin <= MAXLINE)
            {
                        for(i=0 ; i < (MAXLINE - curlin) ; i++)
                                    printf("   "); /* 3 spaces */

                        for(i=1;i<=curlin;i++)
                        {
                                    if(i<=9)
                                                printf("%d  ",i);  /* 1 digit followed by 2 spaces */
                                    else
                                                printf("%d ",i);  /* 2 digits followed by 1 space */
                        }
                        i-=2;
                        for( ; i>=1 ; i--)
                        {
                                    if(i<=9)
                                                printf("%d  ",i);  /* 1 digit followed by 2 spaces */
                                    else
                                                printf("%d ",i);  /* 2 digits followed by 1 space */
                        }
                        printf("\n");
                        curlin++;
            }
            getch();
}





/*
            =====OUT PUT=====
                        1
                     1  2  1
                  1  2  3  2  1
               1  2  3  4  3  2  1
            1  2  3  4  5  4  3  2  1
*/

0 comments:

Post a Comment