Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Thursday, July 23, 2020

C Program to check even or odd number

C Program to check whether a number is even or odd 


Write a C program to input a number from user and check whether a given number is even or odd using if...else. Let's see the below step by step logic check even or odd number.

Example:

  1. INPUT
  2. Enter any number to check even or odd: 15

  OUTPUT
15 is a odd number.

Required knowledge for this exercise

Logic to check whether a given number is even or odd:

Even or odd: If any number which is exactly divisible by 2 with zero remainder then number is even otherwise odd. 

We can use modulo operator % to check whether a number is exactly divisible by 2 or not. 
For example: if(num % 2 == 0) i.e if the number modulo division 2 is true then the number is even otherwise odd.

Now let's see the below step by step logic to check divisibility of a number.

1) Input a number from user and store it in variable num using scanf() function.
2) Check the given number is exactly divisible by 2or not using modulo % operator.
Like if(num % 2 == 0) if it's remainder is equal to0then number is even otherwise odd. Since we know that, modulo %operator always returns remainder.

FlowChart to check Even or odd Number | Protec

Program to check whether a number is even or odd using if...else: 

  1. /* C program to check whether a number is odd or even */ #include <stdio.h> int main() { int num; /* Input number from user */ printf("Enter any number to check even or odd: "); scanf("%d", &num); /* Check if the number is divisible by 2 then it is even */ if(num % 2 == 0) { /* If it's remainder is zero then */ printf("%d is Even number.", num); } else { /* If it's remainder is Nonezero then */ printf("%d is Odd number.", num); } return 0; }

Note: As you can see in above program if and else body contains only single statement. Hence, you can ignore braces { } after if and  else statement. 

For Example:

if(num % 2 == 0)
/* num % 2 is 0 */
printf("%d Number is Even.", num);
else
/* num % 2 is 1 */
printf("%d Number is Odd.", num);

Output:

 Enter any number to check is even or odd: 16
 16 is even number.

 Enter any number to check is even or odd: 11
 11 is odd number.





Next topic >> to be updated soon.

Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

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.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us