C Program to Enter an ODD number between 1 and 49.

How to write a C Program to Enter an ODD number between 1 and 49 in C Programming Language ?


Solution For C Program:

#include<stdio.h>

int main()
{
int x, y, z, n;//initialize variables
printf("Enter an ODD number between 1 and 49:"); // Asks user for input
scanf("%d", &n);// scans user input
if (n % 2 == 0 || n<1 || n>49)//Validates User Input
{
printf("Error: Please enter an ODD integer between 1 and 49\n"); return -1;
}
else
n = (n + 1) / 2;
for (x = 1; x <= n; x++)
{
for (y = n; y >= x; y--)
printf("%s", " ");
for (z = 1; z <= (2 * x - 1); z++)
printf("%s", "*");
puts("");
}
for (x = 1; x <= (n - 1); x++)
{
for (y = 0; y <= x; y++)
printf("%s", " ");
for (z = (2 * n - 3); z >= (2 * x - 1); z--)
printf("%s", "*");
puts("");
}
return 0;
}


Learn More :