Program to find if a number is EVEN or ODD using modulus operator

In my previous posts I had explained the basic operators along with the basic syntax and codes that are used in C and C++ programming language. In this posts I am going to brief you about The MODULUS operator, which is one of the most important operator in any programming language - be it C, C++, C#, JAVA etc.
I shall also explain to you a small and a basic program using modulus operator - to find out if a number is even or odd which shall clear your doubts on the functionality of the mod operator.
The modulus operator is one of the most important arithmetical operator that is used to carry out mathematical tasks and operations. This operator finds its use in returning the remainder between two numbers. The Modulus operator '%' is also called the remainder operator due to the ability to return the remainder between any two numbers.


Use of Modulus Operator:

The Modulus (sometimes called modulo) operation finds the remainder of division of one number by another.
For example, consider two positive integers, 'a', the dividend and 'n', the divisor. The function "a modulus n" (abbreviated as "a mod n" or "a % n") returns the remainder of the Euclidean division of a by n. For instance, the expression "15 mod 2" would evaluate to 1 because 15 divided by 2 leaves a quotient of 7 and a remainder of 1. Similarly "27 % 3" would evaluate to 0 because the division of 27 by 3 leaves a quotient of 9 and leaves a remainder of 0 i.e. there is nothing to subtract from 27 after multiplying 3, 9 times.

The modulo based operations have the following identities:
1. (a mod n) mod n = a mod n
2. n^x mod n = 0for all positive integer values of x.
3. (a + b) mod n = ((a mod n) + (b mod n)) mod n
4. ab mod n = ((a mod n )(b mod n)) mod n
5. a/b mod n = ((a mod n )(b^-1 mod n)) mod n
6. ((a mod n )(b^-1 mod n)) mod n = a mod n


One of the best use of the MODULUS operator is to find if a number is even or odd. The following is the algorithm fo the above problem:

Step 1: Read a number 'a'
Step 2: if 'a' mod 2 is 0 it is even
Step 3: Else it is odd

Implementing the above algorithm to write a program in C or C++ to check if a number is even or odd:

Using C Language:


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a number");
scanf("%d",&a);
//check if the number 'a' is even
if(a % 2 == 0)
printf("%d is an even number",a);
//executed only when the above condition is false
else
printf("%d is an odd number",a);
getch();
}

Using C++ Programming Language:


#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();
cout<<"Enter a number"; cin>>a;
//check if the number 'a' is even
if(a % 2 == 0)
cout<<a<<" is an even number";
//executed only when the above condition is false
else
cout<<a<<" is an odd number";
getch();
}

I have already explained the concepts and basics of writing a program in C or C++ programming language with syntax in my previous posts. Click here to know more...
The a % 2 gives the remainder on dividing a by 2. If a % 2 gives a remainder of 0, it obviouly means the number 'a' is an even number. This was checked using the "==" operator which checks for the equality between two expressions i.e. "a % 2" is matched with '0', thus returning TRUE of FALSE.
On the counter part if (a % 2 != 0) then the number is an odd number.
Note: You can include the condition if(a % 2 != 0) after the else statement. But even with out ths statement, the else part would only be executed when the number is NOT even. For other advanced examples on modulus keep visiting CyboBytes.com


Thanks for reading this post on MODULUS operator - uses and identities and to write a program to find if a number is EVEN or ODD using modulus operatorin C / C++ programming language.
Comment below if you have any doubts.

Comments

Popular posts from this blog

Samsung Galaxy Note 3 - Full Specifications

Run Borland Turbo C/C++ in Android Devices