Showing posts with label Number triangle. Show all posts
Showing posts with label Number triangle. Show all posts

Monday, 29 October 2012

Number Pyramid

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

54321
5432
543
54
5

Ans.

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

The output of above program would be:

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

Sunday, 2 September 2012

Number pyramid

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

123456654321
1234554321
12344321
123321
1221
11

Ans.

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

/**************Output**************/

Output of number pyramid C program
Screen shot for number pyramid C program


Related Programs:

Q. Write a C program to print the following character pyramid.

ABCDEFFEDCBA
ABCDEEDCBA
ABCDDCBA
ABCCBA
ABBA
AA

Tuesday, 24 July 2012

Number pyramid

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


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


Ans.


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


/************Output************/


Output of C program for number pyramid C program
Screen shot for number pyramid C program