Thursday, March 28, 2013

Static Number Pyramid

Q. Write a C program to print the following number triangle.

    1
   232
  34543

Ans.

/*static number pyramid*/
#include<stdio.h>
int main()
{
 int num=3,r,c,x,z,k,sp;
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>0; sp--)
     printf(" ");
  for(c=1,z=r; c<=r; c++,z++)
     printf("%d",z);
  for(k=2,x=r+1; k<=r; x--,k++)
     printf("%d",x);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

    1

   232
  34543

Monday, March 25, 2013

Double Number Triangle

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

1
11
2222
333333
44444444
5555555555

Ans.

/*c program for double number triangle*/
#include<stdio.h>
int main()
{
 int r,c,num;
 printf("Enter any number : ");
 scanf("%d", &num);
 printf("1\n");
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r*2; c++)
     printf("%d",r);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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