C program for checking a number even or odd
Here we will write a C program to check a user input number, whether it is odd or even. Before we start writing the actual code we should know what is an odd number and what is an even number so that we can apply our logic.
An odd number is that integer which is not a multiple of 2 and on dividing by 2 the output is a fraction.Odd numbers are of form n=2e+1
An even number is an integer of form n=2e, where e could be any integer(positive or negative). An even number can be completely divided by 2 ie. remainder is zero.
So we can use modulus operator (%) for checking the evenness or oddness of any integer. If some integer divided by 2 gives any remainder then it is an odd number or if the remainder is zero then it will be an even number.
Remember if any integer is not even then it will be odd and vice versa. So we will check only for one condition, if that condition is true then other one will be false and if the first condition is false then the other one will be true.
Program:-
An even number is an integer of form n=2e, where e could be any integer(positive or negative). An even number can be completely divided by 2 ie. remainder is zero.
Method One:Writing a C program for checking an even or odd number using modulus operator
So we can use modulus operator (%) for checking the evenness or oddness of any integer. If some integer divided by 2 gives any remainder then it is an odd number or if the remainder is zero then it will be an even number.
Remember if any integer is not even then it will be odd and vice versa. So we will check only for one condition, if that condition is true then other one will be false and if the first condition is false then the other one will be true.
Program:-
#include <stdio.h> #include <conio.h> void main(){ //declare an integer n. int n; //ask the user for an integer to check printf("Enter the integer you want to check\n"); //scan the value entered from keyboard and put it in the variable n. scanf("%d",&n ); if(n%2==0) printf("%d is an even integer" ,n ); else printf("%d is an odd integer" ,n ); }
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment