Write a program to identify if the number is even or odd

04 May 2022

Write a program to identify if the number is even or odd


Description

Get a number as input from the user and check whether the given number is odd or even

Input

10

Output

Even

Input

5

Output

Odd

C Program

Method 1

#include <stdio.h>

int main()

{

    int num;

    printf ("Enter a number: ");

    scanf ("%d", &num);

    if (num % 2 == 0)

        printf ("Even number");

    else

        printf ("Odd number");

    return 0;

}

 

Method 2

#include <stdio.h>

int main()

{

    int num;

    printf ("Enter a number: ");

    scanf ("%d", &num);

    num%2 == 0? printf("Even number"):printf("Odd number");

    return 0;

}

 

Method 3

#include <stdio.h>

int main()

{

    int num,result;

    printf ("Enter a number: ");

    scanf ("%d", &num);

    if(num & 1)

    result=1;

    else

    result =0;

    (result==1)? printf("Odd") : printf("Even");

    return 0;

}


C++ Program

Method 1

#include <iostream>

using namespace std;

int main()

{

    int num;

    cout<<"Enter a number: ";

    cin>>num;

    if (num % 2 == 0)

        cout<<"Even number";

    else

        cout<<"Odd number";

    return 0;

}

 

Method 2

#include <iostream>

using namespace std;

int main()

{

    int num;

    cout<<"Enter a number: ";

    scanf ("%d", &num);

    num%2 == 0? cout<<"Even number":cout<<"Odd number";

    return 0;

}

 

Method 3

#include <iostream>

using namespace std;

int main()

{

    int num,result;

    cout<<"Enter a number: ";

    cin>>num;

    if(num & 1)

    result=1;

    else

    result =0;

    (result==1)? cout<<"Odd" : cout<<"Even";

    return 0;

}


Java

Method 1

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number : ");

int num = sc.nextInt();

     if (num % 2 == 0)

              System.out.println("Even number");

     else

              System.out.println("Odd number");

      }

}

 

Method 2

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.print("Enter the number : ");

    int num = sc.nextInt();

        String result = num % 2 == 0 ? "Even number" : "Odd number";

        System.out.println (result);

      }

}


Python

Method 1

num = int(input("Enter a number:")) 

if num % 2 == 0: 

  print("Even number ") 

else: 

  print("Odd number ")

 

Method 2

num = int(input("Enter a number:")) 

print("Even number") if num%2 == 0 else print("Odd number")

 

Method 3

num = int(input("Enter a number:"))

if num&1:

    print("Odd")

else:

    print("Even")

Related Articles

Ask Us Anything !