Program to Check Whether a Number is Even or Odd [C, C++, Python & Java]

Posted in

Program to Check Whether a Number is Even or Odd [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on March 29, 2024

    This tutorial will help you write a program in C, C++, Python, and Java to check whether a user-entered number is even or odd.

    To write this program, you must know the programming language syntax and the if...else statement.

    So, let us get started!

    What is an Even and Odd Number?

    Odd Number: Any natural number expressed in the form of (2n+1), where n is a natural number, is an odd number. In other words, it is a number not divisible by 2.

    Examples of odd numbers include 1, 3, 5, 7, 9, etc.

    Even Number: Any natural number expressed in the form of 2n, where n is a natural number, is an even number. In other words, it is a number completely divisible by 2.

    Examples of even numbers include 2, 4, 6, 8, etc.

    C Program to Check Whether the Number is Even or Odd

    #include<stdio.h>
    int main()
    {
       int num;
       printf("Enter a number: ");
       scanf("%d", &num);
       if(num%2==0)
          printf("It is an even number.");
       else
          printf("It is an odd number.");
        return 0;
    }

    Output

    Enter a number: 56
    It is an even number.

    C++ Program to Check Whether the Number is Even or Odd

    #include<iostream>
    using namespace std;
    int main()
    {
       int num;
       cout<<"Enter a number: ";
       cin>>num;
       if(num%2==0)
          cout<<"It is an even number.";
       else
          cout<<"It is an odd number.";
    }

    Output

    Enter a number: 67
    It is an odd number.

    Python Program to Check Whether the Number is Even or Odd

    num=int(input("Enter a number: "))
    if num%2==0:
        print("It is an even number")
    else:
        print("It is an odd number")

    Output

    Enter a number: 123
    It is an odd number

    Java Program to Check Whether the Number is Even or Odd

    import java.util.*;
    public class Main
    {
        public static void main(String[] args)
        {
            int num;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a number: ");
            num = sc.nextInt();
            if(num%2==0)
                System.out.println("It is an even number.");
            else
                System.out.println("It is an odd number.");
        }
    }

    Output

    Enter a number: 
    78
    It is an even number.

    Conclusion

    You might have found it easy to write a program in C, C++, Python, and Java to check whether a number is even or odd. It is an entry-level program where individuals implement to learn a specific programming language.

    If you face any difficulties, do let us know in the comments. We would be glad to help you.

    People are also reading:

    Leave a Comment on this Post

    0 Comments