Question:
Write a C program to check whether a number is even or odd.
Answer:
PROGRAM

#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if(number % 2 == 0)
printf("\nThe number %d is even.", number);
else
printf("\nThe number %d is odd.", number);
return 0;
}
Program Working:
Its a simple and easy to understand program code. I'll explain it step by step, so u can easily understand the working.
* An integer variable 'number' is declared to store the user input.
* User is asked to input an integer number to check whether it is an even or odd number.
* The user input is read from the keyboard and is stored into the variable number using scanf statement.
* Even numbers are divisible by 2. So to check whether a number is even , the number is divided by 2 and the remainder is obtained. If the remainder is 0 (zero) , the number is even. Else the number is odd.
So to do this C language uses an operator % (modulus or modulo division) which gives the remainder of a division. So the number given by the user is divided by 2 and the remainder is checked.
If the remainder is equal to 0, the program prints the number is even
* If remainder is not 0, the program prints the number is odd.
* The program stops.
0 Comments: