Chapter4
This commit is contained in:
49
DeitelC/Chapter4/test2.c
Normal file
49
DeitelC/Chapter4/test2.c
Normal file
@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int odd;
|
||||
|
||||
// Prompt user for input
|
||||
puts("Enter an odd number: ");
|
||||
if (!scanf("%d", &odd) || odd % 2 == 0) {
|
||||
puts("Enter a valid odd integer!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Print the diamond pattern
|
||||
int mid = odd / 2; // Middle point of the diamond
|
||||
|
||||
// Upper half of the diamond
|
||||
for (int i = 0; i < mid; i++) {
|
||||
// Print leading spaces
|
||||
for (int j = 0; j < mid - i; j++) {
|
||||
printf(" ");
|
||||
}
|
||||
// Print asterisks
|
||||
for (int j = 0; j < 2 * i + 1; j++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Middle line of the diamond
|
||||
for (int i = 0; i < odd; i++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Lower half of the diamond
|
||||
for (int i = mid - 1; i >= 0; i--) {
|
||||
// Print leading spaces
|
||||
for (int j = 0; j < mid - i; j++) {
|
||||
printf(" ");
|
||||
}
|
||||
// Print asterisks
|
||||
for (int j = 0; j < 2 * i + 1; j++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user