Showing posts with label c program for number triangle. Show all posts
Showing posts with label c program for number triangle. Show all posts

7.07.2012

Number pyramid/triangle

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

54321
 4321
  321
   21
    1

Ans.

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

/*********Output**********/
Output of C program for number triangle
Screen shot for number triangle

2.21.2012

Number rhombus2

Q. Write a C program to print the number in following fashion:
      1
     212
    32123
   4321234
  543212345
   4321234
    32123
     212
      1


Ans.


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


/*************** OUTPUT ***************/





2.01.2012

Number triangle equal

Q. write a C following number triangle :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5


Ans.


/*c program for number structure*/
#include<stdio.h>
#include<conio.h>
int main()
{
  int num,r,c;
  printf("Enter structure number : ");
  scanf("%d", &num);
  for(r=1; r<=num; r++)
  {
     for(c=1; c<=r; c++)
        printf("%d",r);
     printf("\n");
  }
  return 0;
}
Output:-
Enter structure number : 5

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5