Sunday, April 21, 2013

Character - Symbol Pyramid

Q. Write a C program to print the following character and symbol design:

A
**
ABC
****
ABCDE
****
ABC
**
A

Ans.

/*c program for character and star pyramid*/
#include<stdio.h>
int main()
{
 char ch,r,c,q;
 printf("Enter ending character : ");
 scanf("%c", &ch);
 if(ch>='a' && ch<='z')
    ch=ch-32;
 for(r='A'; r<=ch; r++)
 {
  for(c='A'; c<=r; c++)
  {
    if(r%2==0)
       printf("*");
    else
       printf("%c",c);
  }
  printf("\n");
 }
 for(q=ch-1; q>='A'; q--)
 {
  for(c='A'; c<=q; c++)
  {
    if(q%2==0)
       printf("*");
    else
       printf("%c",c);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:
Output of character symbol pyramid C program
Figure: screen shot for character star pyramid C program

-------------------------------------------


Q. Write a C program to print the following number and symbol design:

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

Ans.

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

The output of above program would be:


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

No comments:

Post a Comment