Q:

C program to check if number is odd or even

belongs to collection: C programs - Basic C Programs

0
Write program is to check whether the number provided by the user is even or odd. 
As you aware that even numbers are completely divisible by zero i;e
they leave remainder zero when divided by two.
On the other hand, odd numbers are not completely divisible by two. So,
we can make use of the remainder operator(%) in c to check whether a number is odd or even.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C program to check if number is odd or even - Source code

Program to check whether the number is even or odd using the modulus operator (%)
#include<stdio.h>
int main()
{
   int n;
 
   printf("Enter an integer: ");
   scanf("%d",&n);
 
   // Modulus (%) returns remainder
   if ( n%2 == 0 )   // returns true if remainder is 0
      printf("%d is an even number", n);
   else
      printf("%d is an odd number", n);
 
   return 0;
}

Program Output

 

 

Case 1:

Enter an integer: 6723
6723 is an odd number


Case 2:

Enter an integer: 4578
4578 is an even number

Program Explanation

Modulus operator in C programming language, denoted by %, returns the remainder after the division operation. Here we have perform the modulus operator on the number provided by the user and checked whether it is equal to zero(n%2 == 0). The if statement is executed automatically if the expression inside it returns true value. Otherwise, the else statement is executed.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to check whether a number is positive or... >>
<< C program to swap two numbers using a third variab...