C Programming

C Programming


Equilateral Triangle Number Design

Posted: 05 Sep 2013 10:12 AM PDT

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

     5
    454
   34543
  2345432
 123454321

Ans.

/*c program for equilateral triangle design*/
#include<stdio.h>
int main()
{

 int num,r,c,sp,n;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(n=num-r+1; n<=num; n++)
     printf("%d",n);
  for(c=1,n=num-1; c<r; c++,n--)
     printf("%d",n);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Equilateral Triangle design C program
Figure: Screen shot for Equilateral Triangle design C program


Z-Shape Pyramid

Posted: 05 Sep 2013 08:18 AM PDT

Q. Write a C program to print the Z-Shape pyramid as:

******
    *
   *
  *
 *
******

Ans.

/*c pyramid program for z-shape*/
#include<stdio.h>
int main()
{

 int num,r,c,sp,n;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
    printf("*");
 printf("\n");
 for(r=1,n=num-1; r<=num-2; r++,n--)
 {
  for(sp=1; sp<n; sp++)
     printf(" ");
  printf("*");
  printf("\n");
 }
 for(r=1; r<=num; r++)
    printf("*");
 printf("\n");
 getch();
 return 0;
}

The output of above program would be:

Output of Z-Sahpe pyramid C program
Figure : Screen shot for Z-Shape pyramid C program

Number Triangle

Posted: 05 Sep 2013 07:01 AM PDT

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

     1
    21
   321
  4321
 54321

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{

 int num,r,c,sp;
 printf("Enter no of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(c=r; c>=1; c--)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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



Post a Comment