C Programming

C Programming


Nested Pyramid

Posted: 15 May 2013 03:03 AM PDT

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

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

Ans.

/*c program for nested pyramid*/
#include<stdio.h>
int main()
{
 int r,c,num=3,n;
 n=num;
 for(r=1; r<=num; r++,n--)
 {
   for(c=1; c<=r; c++)
       printf("*");
   printf(" ");
   for(c=n; c>=1; c--)
       printf("*");
   printf(" ");
   for(c=n; c>=1; c--)
       printf("*");
   printf(" ");
   for(c=1; c<=r; c++)
       printf("*");
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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

Dignal Character Pyramid

Posted: 15 May 2013 02:26 AM PDT

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

 a b b b
 b a b b
 b b a b
 b b b a

Ans.

/*c program for alternate character pyramid*/
#include<stdio.h>
int main()
{
 char r,c,ch='d';
 for(r='a'; r<=ch; r++)
 {
  for(c='a'; c<=ch; c++)
  {
    if(r==c)
       printf(" a");
    else
       printf(" b");
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of dignal equal pyramid C program
Figure: Screen shot for dignal equal pyramid C program

Related program:

1. Print the following dignal number pyramid:

  1 2 2 2
  2 1 2 2
  2 2 1 2
  2 2 2 1

Alternate Number Pyramid

Posted: 15 May 2013 02:28 AM PDT

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

1222
2122
2212
2221

Ans.

/*c program for alternate number pyramid*/
#include<stdio.h>
int main()
{
 int r,c,num=4;
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=num; c++)
  {
    if(c==r)
      printf("1");
    else
      printf("2");
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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

Related program:

1. Print the dignal character pyramid as:

 a b b b
 b a b b
 b b a b
 b b b a



Post a Comment