Wednesday, 17 April 2013

Reverse Number Pyramid

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

10 9 8 7
6  5 4
3  2
1

Ans.

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

The output of above program would be:

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

Thursday, 4 April 2013

Number star pyramid

Q. write a C program to print the following number star pyramid as:

1
**
123
****
12345
******

Ans.

/* c program for number star pyramid */
#include<stdio.h>
int main()
{
 int num=6,r,c;

 for(r=1; r<=num; r++)
 {
   for(c=1; c<=r; c++)
   {
      if(r%2==0)
         printf("*");
      else
         printf("%d",c);
   }
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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

Monday, 1 April 2013

Square Star Pyramid

Q. Write a C program to print the square star pyramid design as:

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

Ans.

/*c program for star square design*/
#include<stdio.h>
int main()
{
 int cols,rows,r,c,sp;
 printf("Enter no. of columns: ");
 scanf("%d", &cols);
 printf("Enter no. of rows: ");
 scanf("%d", &rows);
 for(r=1; r<=cols; r++)
 {
  if(r==1 || r==cols)
      printf(" ");
  else
      printf("*");
 }
 printf("\n");
 for(c=1; c<=rows-2; c++)
 {
   printf("*");
   for(sp=1; sp<=cols-2; sp++)
       printf(" ");
   printf("*\n");
 }
 for(r=1; r<=cols; r++)
 {
   if(r==1 || r==cols)
       printf(" ");
   else
       printf("*");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of Square Star Pyramid C program
Figure: Screen shot for Square Star Pyramid C program

Rectangular Star Pyramid

Q. write a c program to print the following star rectangle design:

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


Ans.

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

The output of above program would be:

Output of Rectangle Star Pyramid C program
Figure: Screen shot of Rectangle Star pyramid C program

Star sequence pyramid

Q. Write a C program to print the following symbol (star) pyramid.

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

Ans.

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

The output of above program would be:

Enter no. of rows: 6
*
**
***
****
*****
******

Thursday, 28 March 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, 25 March 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