C program to check number is even or odd
C program to check given number is even or odd
Write a C program to input any number from user and check whether the given number is even or odd using if...else. Let's see the below step by step logic check whether the given number is even or odd using if...else.
Before writing any program you just imagine about output screen first. Like what input to be given, and output to be displayed. Let's have a look at the below example.
- INPUT
- Enter any number to check even or odd: 15
OUTPUT
15 is odd number.
Required knowledge for this exercise
Logic to check whther number is even or odd using if...else:
Remember: If any number is exactly divisible by 2
then it is called even number otherwise odd.
1) Input any number from user and store it in variable num
using scanf()
2) Check the given number is exactly divisible by 2
or not using modulo %
operator. Likeif(num % 2 == 0)
if it's remainder is equal to0
then number is even otherwise odd. Since we know that, modulo %
operator always returns remainder.
Program to chek whether a number is even or odd using if...else:
-
#include <stdio.h>
int main()
{
int num;
printf("Enter any number to check even or odd: ");
scanf("%d", &num);
if(num % 2 == 0)
{
printf("d% is Even number.", num);
}
else
{
printf("%d is Odd number.", num);
}
return 0;
}
Please note: if(num % 2 == 0) Here, symbol%
is a modulo operator not a percent sign.
Output:
- Enter any number to check even or odd: 15
- 30 is odd number.
Help others by sharing this page.
Ahmad Irshad
Author & Editor
I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.