Hello Coders,


In this post we are going to learn about the program that checks whether the number is even or odd  in different programming languages. Such as C++, Java and Python. Let us first learn what is Even and Odd Numbers.

Even Numbers are those Numbers which are completely divisible by 2.

Odd Numbers are those Numbers which are not completely divisible by 2  i.e. always there will be 1 in the remainder.

In programming languages we require some logic to execute certain operations son in order to check for even and odd numbers we have to give some command/logic to the computer so that it will perform accordingly.


LOGIC:
As we all know that all the even numbers are completely divisible by 2.
This is the formula that helps to check whether the number is divisible by 2 or not.
(n % 2 == 0)
here, 'n' is the input number,
% = This is known as the modulus operator. It is similar to division '/' operator,. But the significance of this operator is that it will give the remainder as output. The division operator divides the number and gives the quotient as the output but the modulus operator will give the remainder as the answer or output.

Now if the value of n%2 is equal to 0 then the number will be Even Number because it is completely divisible 2 by giving the remainder as 0.
And if not then the number will be Odd because it is not completely divisible by 2.

Given below is the code for Four programming languages, in that code bits some comments are added please read it carefully to understand the syntax for the languages.
We will be learning this program in four different programming languages C, C++, Java and Python.
 
So let's get started.

C


 #include <stdio.h>
 int main()
 {   
     //Declaring variables
     int n; 
     // Asking the user to enter the value
   	 printf("Please enter the number you want to check for even an odd");
     //Storing the input value in variable 'n'
     scanf("%d", &n);
     // This is the conditional statement which check the Main Logic
     if (n %2 == 0)
     {
     	 // If the condition becomes true then this statement will be printed
         printf(" The entered number is Even Number");
     }
     else 
     {
     	 // If the above condition becomes false then this statement will be printed
         printf("The enterd number is Odd Number");
     }
     return 0;
 }
            


    

Output for C




C++


//Including Header files
#include <iostream>
using namespace std;

//Main function of program
 int main()
 {
 	// declaring the variable
    int n;
    // Asking the user to enter the value
    cout<<"Please enter the value that is to be checked for even and odd";
    // Storing the input value in variable 'n'
    cin>>n;
    // This is the conditional statement which check the Main Logic
    if (n % 2 == 0)
    {
    	// If the condition becomes true then this statement will be printed
        cout<<"The Entered number is Even";
    }
    else
    // If the above condition becomes false then this statement will be printed
    cout<<"the entered number is odd";
    return 0;

}         


    

Output for C++


Java


 package com.company;

 // Import java util package this will help to use Scanner class
 import java.util.*;

 //Class Main
 //Starting point of program
 public class Main
 {

    //This is the main Function
    public static void main(String[] args)
    {

       Scanner sc = new Scanner (System.in);

      System.out.print("Please Enter the number you want to check for even and odd ");
        int num = reader.nextInt();

        if(num % 2 == 0)
            System.out.println("The Entered Number is Even Number");
        else
            System.out.println( " The Entered Number is Odd Number");
    }
}
    


    

Output for Java






Python


# even odd program
# Taking the input from the user.
n= int(input("Please enter the number to check for even odd"))
# This is the conditional statement which check the Main Logic
if n%2 == 0:
	# If the condition becomes true then this statement will be printed
    print("The entered number is Even number")
else:
	# If the condition becomes false then this statement will be printed
    print("The entered number is Odd Number")


    

Output for Python





//ENJOY CODING\\

1 Comments

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post