easy
9 views
Print Odd or Even (Using if-else)
Determine if a given integer is odd or even and print the corresponding result.
Understand the Problem
Problem Statement
The program must accept an integer N as the input. The program must print Odd if N is odd. Else the program must print Even as the output.
Constraints
- N is an integer
- -10^9 ≤ N ≤ 10^9
- The input will contain exactly one integer
Examples
Example 1
Input
10Output
EvenExplanation
Since 10 is divisible by 2 (10 % 2 = 0), the number is even, so we print "Even".
Example 2
Input
7Output
OddExplanation
Since 7 is not divisible by 2 (7 % 2 = 1), the number is odd, so we print "Odd".
Example 3
Input
-3Output
OddExplanation
Since -3 is not divisible by 2 (-3 % 2 = -1), the number is odd, so we print "Odd".
Solution
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
if (N % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 0;
}Time:O(1)
Space:O(1)
Approach:
The C solution uses scanf to read the integer N from standard input. It then uses the modulo operator (%) to check if N is divisible by 2. If N % 2 == 0, it prints "Even" using printf. Otherwise, it prints "Odd". The program returns 0 to indicate successful execution.
Visual Explanation
Loading diagram...