C program to check whether a given number (integer) is Even or Odd.
Test cases | Input | Output |
---|---|---|
Test Case 1 | 24 | 24 is even. |
Test Case 2 | -25 | -25 is odd. |
In this program, we will check if a given number is odd or even by the help of c programming
At first, we will assign a variable named 'number'. It will be of integer data-type here in our code.
int number;
Then we will store the input number giver by the user into our program using the 'scanf' function. The program will use this stored number to execute its operations i.e. checking if it is zero, positive or negative number.
scanf("%d", &number);
Now Now we will use an 'if' statement in our next step and give the condition to check whether the number which user gave as an input when divided by 2 gives remainder zero or not.
if(number % 2 == 0)
If the remainder comes out to be zero when the input number is divided by 2. Then by using 'printf' command our code will print in the output console that "[number] is even".
printf("%d is even.", number);
Now, if the remainder does not comes out to be zero when the input number is divided by 2. Then by using 'printf' command our code will print in the output console that "[number] is odd".
else
printf("%d is odd.", number);
Above we have provided you with two sample cases, when we try the first sample case i.e. input 24 then our code divides the number 24 by 2 which gives the remainder equal to zero that's why we will get the output as "24 is even".
Above we have provided you with two sample cases, when we try the first sample case i.e. input -25 then our code divides the number -25 by 2 which gives the remainder equal to zero that's why we will get the output as "-25 is odd".
I hope with the help of this explanation you understood how we can find if a number is odd or even by C Programming.
The code is provided below for your help and also a video tutorial is given for your better experience.
0 Comments