Summary: This C program reads two integers as input and iterates through all integers between them (inclusive). It prints the English word for numbers 1 through 9, and for numbers greater than 9, it prints whether they are even or odd.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main() {
int a, b;
scanf("%d\n%d", &a, &b);
// Loop from a to b (inclusive)
for (int n = a; n <= b; n++) {
if (n >= 1 && n <= 9) {
// Print the English representation for numbers 1 to 9
switch(n) {
case 1: printf("one\n"); break;
case 2: printf("two\n"); break;
case 3: printf("three\n"); break;
case 4: printf("four\n"); break;
case 5: printf("five\n"); break;
case 6: printf("six\n"); break;
case 7: printf("seven\n"); break;
case 8: printf("eight\n"); break;
case 9: printf("nine\n"); break;
}
} else {
// For numbers greater than 9, print "even" if the number is even, otherwise print "odd"
if (n % 2 == 0) {
printf("even\n");
} else {
printf("odd\n");
}
}
}
return 0;
}
Input Reading:
The program uses scanf()
to read two integer inputs, representing a range of values to evaluate.
Looping Through Range:
A for
loop is used to iterate from a
to b
(inclusive), ensuring that each number in the range is processed.
Switch Statement:
A switch
block handles numbers from 1 to 9, printing their English equivalents. This avoids repetitive if-else
comparisons and offers clean readability.
Even/Odd Classification:
For numbers greater than 9, the program uses the modulo operator %
to check if the number is divisible by 2. If so, it prints even
, otherwise odd
.
Conditional Structure:
The program demonstrates the use of combined logic with relational and logical operators like >=
, <=
, and &&
.
Use of Header Files:
While only stdio.h
is required for this specific program, other headers are harmless here but unnecessary for functionality.