Question
Question: If the user gives an even number (an invalid input), I want the program asks for the input again without running it one more
Question: If the user gives an even number (an invalid input), I want the program asks for the input again without running it one more time. Would you kindly help me to fix it?
An example of the output should look like this:
please enter number of rows (odd nummber): 4 please enter number of rows (odd nummber): 7 * *** ***** ******* ***** *** *
Programming c++:
#include
int main() {
int rows, i, j, k;
printf("please enter number of rows (odd nummber):");
scanf("%d", &rows);
// Ensure that the number of rows entered is odd
if (rows % 2 == 0) {
printf("please enter number of rows (odd nummber): ");
return 0;
}
// Print upper half of diamond
for (i = 1; i <= rows; i += 2) {
// Print spaces
for (j = 1; j <= (rows - i) / 2; j++) {
printf(" ");
}
// Print asterisks
for (k = 1; k <= i; k++) {
printf("*");
}
printf(" ");
}
// Print lower half of diamond
for (i = rows - 2; i >= 1; i -= 2) {
// Print spaces
for (j = 1; j <= (rows - i) / 2; j++) {
printf(" ");
}
// Print asterisks
for (k = 1; k <= i; k++) {
printf("*");
}
printf(" ");
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started