-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathexercise05.c
61 lines (50 loc) · 1.1 KB
/
exercise05.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// C Primer Plus
// Chapter 6 Exercise 5:
// Have a program request the user to enter an uppercase letter. Use nested loops
// to produce a pyramid pattern like this:
// A
// ABA
// ABCBA
// ABCDCBA
// ABCDEDCBA
// The pattern should extend to the character entered. For example, the preceding
// pattern would result from an input value of E.
#include <stdio.h>
void print_spaces(unsigned int n);
int main(void)
{
char uppercase_letter;
char c1, c2;
do // get uppercase letter from user
{
printf("Enter an uppercase letter: ");
scanf(" %c", &uppercase_letter);
} while (uppercase_letter < 'A' || 'Z' < uppercase_letter);
for(c1 = 'A'; c1 <= uppercase_letter; c1++)
{
// print opening spaces
print_spaces(uppercase_letter - c1);
// print letters
// ascending
for (c2 = 'A'; c2 < c1; c2++)
{
printf("%c", c2);
}
// descending
for (; 'A' <= c2; c2--)
{
printf("%c", c2);
}
// print closing spaces
print_spaces(uppercase_letter - c1);
printf("\n");
}
return 0;
}
void print_spaces(unsigned int n)
{
for (int i = 0; i < n; i++)
{
printf(" ");
}
}