Print Even and Odd Numbers in C
In C programming, you can determine whether a number is even or odd using the modulus operator (%). Even numbers are divisible by 2, while odd numbers are not.
1. Problem Statement
Write a C program that takes an integer input from the user and prints whether it is even or odd.
2. Algorithm
Step 1: Start the program.
Step 2: Declare an integer variable to store the number.
Step 3: Read the number from the user.
Step 4: Check if the number % 2 equals 0.
Step 5: If yes, print 'Even number', else print 'Odd number'.
Step 6: End the program.
3. C Program
C
Program to print whether a number is even or odd
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num % 2 == 0) {
printf("%d is an Even number", num);
} else {
printf("%d is an Odd number", num);
}
return 0;
}
4. Example
Input: Enter a number: 7
Output: 7 is an Odd number
Notes
Even numbers are divisible by 2 without remainder. Odd numbers have a remainder of 1 when divided by 2. The modulus operator (%) is used to determine this in C.
Codecrown