2.01.2014

Character Pyramid

Q. Write a C program to takes the last character by user and print the following character pyramid as:
(if user input : j), then output would be like as:

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGHJHGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGFEDCBA
ABCDEFEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A

Ans.

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

 char ch,r,c,p;
 printf("Enter any character : ");
 scanf("%c", &ch);
 if(ch>='a' && ch<='z')
   ch=ch-32;
 for(r='A'; r<=ch; r++)
 {
   for(c='A'; c<=r; c++)
      printf("%c",c);
   for(c=r-1; c>='A'; c--)
      printf("%c",c);
   printf("\n");
 }
 for(p=ch-1,r='A'; r<ch; r++,p--)
 {
   for(c='A'; c<=p; c++)
      printf("%c",c);
   for(c=p-1; c>='A'; c--)
      printf("%c",c);
   printf("\n");
 }
 getch();
 return 0;
}

/*********************************************************
The output of above program would be:
*********************************************************/


Output of character pattern pyramid C program
Figure: Screen shot for character pyramid C program

1 comment:

  1. hey sir... plzz write the source code for a program which decompress a string like input= B10A1
    ouput= BBBBBBBBBBA
    2) Write a program to print a histogram on the basis of given integer
    fr eg: [-2 2 3 -1]
    output
    **
    **
    ***
    *

    ReplyDelete