Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Even or Odd Numbers

Now, with the use of arithmetic operators in coding, we are able to easily determine whether a number is even or odd. This functionality allows for more efficient and accurate programming.

(a.) C Code

#include <stdio.h>

    int main() {
        int num;
        
        printf("Enter a number: ");
        scanf("%d", &num);

        if (num % 2 == 0)
            printf("%d is even.
", num);
        else
            printf("%d is odd.
", num);

        return 0;
    }
Output:-
Enter a number: 7
7 is odd.

(b.) C++ Code

#include <iostream>
    int main() {
        int num;

        std::cout << "Enter a number: ";
        std::cin >> num;

        if (num % 2 == 0)
            std::cout << num << " is even." << std::endl;
        else
            std::cout << num << " is odd." << std::endl;

        return 0;
    }
Output:-
Enter a number: 7
7 is odd.

(c.) Python Code

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

    if num % 2 == 0:
        print(num, "is even.")
    else:
        print(num, "is odd.")
Output:-
Enter a number: 7
7 is odd.

(d.) Java Code

import java.util.Scanner;

    public class EvenOrOdd {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int num = scanner.nextInt();

            if (num % 2 == 0)
                System.out.println(num + " is even.");
            else
                System.out.println(num + " is odd.");

            scanner.close();
        }
    }
Output:-
Enter a number: 7
7 is odd.