Alternate Number Pyramid

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

1222
2122
2212
2221

Ans.

/*c program for alternate number pyramid*/
#include<stdio.h>
int main()
{
 int r,c,num=4;
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=num; c++)

  {
    if(c==r)
      printf("1");
    else
      printf("2");
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of alternate number pyramid C program
Figure: Screen shot for alternate number pyramid C program

Related program:

1. Print the dignal character pyramid as:

 a b b b
 b a b b
 b b a b
 b b b a

Comments