8.23.2013

Alternate Number-Star Pyramid

Q. Write a C program to print the following alternate number-star pyramid/triangle as:

1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1

Ans.

/*c program for alternate number-star pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,z=1,p,n;
 printf("Enter maximum number : ");
 scanf("%d", &num);

 for(r=1; r<=num; r++,z=z+2)
 {
  for(c=1; c<=z; c++)
  {
    if(c%2==0)
       printf("*");
    else
       printf("%d",r);
  }
  printf("\n");
 }
 n=num+3;
/*
NOTE: increase +1 above digit when you increase maximum number to 4 
For example: if you enter:
num=5 then n=num+4
if num=6 then n=num+5 and so on. 
*/
 p=num;
 for(r=num; r>=1; r--,n=n-2,p--)
 {
  for(c=1; c<=n; c++)
  {
    if(c%2==0)
       printf("*");
    else
       printf("%d",p);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Alternate Number-Star Pyramid C program
Figure : Screen shot for Alternate Number-Star
 Triangle C program

No comments:

Post a Comment