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;
}
/*********************************************************
(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:
*********************************************************/Figure: Screen shot for character pyramid C program |
No comments:
Post a Comment