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

Sunday, 4 November 2012

Number Pyramid

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

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

Ans.

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

The output of above program would be:

Output of number pyramid C program
Figure: Screenshot for number pyramid C program


Monday, 17 September 2012

Number Pyramid

Q. Write a C program for print the following number triangle:
or
Q. Write a C program for display the following number pyramid:

 5
 45
 345
 2345
 12345

Ans.

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


The output of above program would be:


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

Number Pyramid

Q. Write a C program for print the following number triangle:
or
Q. Write a C program for display the following number pyramid:

 5
 454
 34543
 23454321
 123454321

Ans.

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


The output of above program would be:


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

Number Triangle


Q. Write a C program for print the following number triangle:
or
Q. Write a C program for display the following number pyramid:

 9
 898
 78987
 6789876

Ans.

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


The output of above program would be:

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

Saturday, 8 September 2012

Number Pyramid

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

       1
      222
     33333
    4444444
   555555555

Ans.

/*c program for number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c,k,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=1; c<=r; c++)
     printf("%d", r);
  for(k=2; k<=r; k++)
     printf("%d", r);
  printf("\n");
 }
 getch();
 return 0;
}

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

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

Tuesday, 24 July 2012

Number structure

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


           1
         1 2 3
       1 2 3 4 5
     1 2 3 4 5 6 7
   1 2 3 4 5 6 7 8 9


Ans.


/*c program for above number triangle codes*/
#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=1; sp<=num-r; sp++)
     printf("  ");   //it is 2 blank space
  for(c=1; c<=2*r-1; c++)
     printf(" %d",c);
  printf("\n");
 }
 getch();
 return 0;
}


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


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

Friday, 23 December 2011

Number pattern

Q. Write a C program to display the following pattern:
 1
 2   3
 4   5    6
 7   8    9    10
11  12   13    14  15

Ans.

/*program to design the above pattern*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int r,c,n=1;
 clrscr();
 for(r=1; r<=5; r++)
 {
  for(c=1; c<=r; r++)
  {
    printf("%3d",n++);
  }
  printf("\n");
 }
 getch();
}

Output:-

 1
 2   3
 4   5    6
 7   8    9    10
11  12   13    14  15