Friday, November 21, 2014

C pyramid pro



C Program to print number border rectangular pyramid

#include

int main()
{
    int number, row, col;
    printf("\nEnter Number of Rows to be display : \n");
    scanf("%d", &number);
    for (row = 1; row <= number; row++)
      {
        for (col = 1; col <= number; col++)
          {
            if (row == 1 || row == number)
            {
                printf("1\t");
            }
            else if (col == 1 || col == number)
            {
                printf("1\t");
            }
            else
               {
                printf("0\t");
            }
        }
        printf("\n");
    }
     return 0;
}

Output

Enter Number of Rows to be display : 6
1    1    1    1    1    1    
1    0    0    0    0    1    
1    0    0    0    0    1    
1    0    0    0    0    1    
1    0    0    0    0    1    
1    1    1    1    1    1

C Program to print zero border rectangular pyramid

#include
#include
int main()
{
    int number, row, col;
    printf("\nEnter Number of Rows to be display : \n");
    scanf("%d", &number);
    for (row = 1; row <= number; row++)
      {
        for (col = 1; col <= number; col++)
         {
            if (row == 1 || row == number)
             {
                printf("0\t");
             }
             else if (col == 1 || col == number)
             {
                printf("0\t");
              }
               else
              {
                printf("1\t");
            }
        }
        printf("\n");
    }
    return 0;
}

Output

Enter Number of Rows to be display : 6
0    0    0    0    0    0    
0    1    1    1    1    0    
0    1    1    1    1    0    
0    1    1    1    1    0    
0    1    1    1    1    0    
0    0    0    0    0    0

C Program to print subtracting rectangular pyramid

#include
#include
int main()
{
    int number, row, col;
    int displayNum;
    printf("\nEnter Number of Rows to be display : \n");
    scanf("%d", &number);
    for (row = 1; row <= number; row++)
     {
        displayNum = number;
        for (col = 1; col <= number; col++)
        {
            printf("%d\t", displayNum--);
        }
        printf("\n");
    }
    return 0;
}

Output

Enter Number of Rows to be display : 5
5    4    3    2    1    
5    4    3    2    1    
5    4    3    2    1    
5    4    3    2    1    
5    4    3    2    1

No comments:

Post a Comment