7.03.2013

Odd Number Pyramid

Q. Write a C program to print the following odd number pyramid as:

1
3 5
5 7 9
7 9 11 13

Ans.

/*c program for odd number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,z=1,p;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++,z=z+2)
 {
  for(c=1,p=z; c<=r; c++,p=p+2)
      printf(" %d",p);
  printf("\n");
 }

 getch();
 return 0;
}

The output of above program would be:


output for odd number pyramid C program
Figure: Screen-shot for odd number pyramid C program


1 comment:

  1. #include
    using namespace std;
    int main()
    {
    int num=4,r,c,z=1,p;

    for(r=1; r<=num; r++)
    {
    for(c=1,p=z; c<=r; c++,p=p+2)
    printf(" %d",p);
    printf("\n");


    }


    system("PAUSE");
    }

    ReplyDelete