Program to Check if Given Number is Odd or Even using Switch Case

Program to Check if Given Number is Odd or Even using Switch Case

  • Write a program to Check if Given Number is Odd or Even using Switch Case in C
  • Write a program to Check if Given Number is Odd or Even using Switch Case in C++
  • Write a program to Check if Given Number is Odd or Even using Switch Case in Python
  • Write a program to Check if Given Number is Odd or Even using Switch Case in PHP
  • Write a program to Check if Given Number is Odd or Even using Switch Case in Java
  • Write a program to Check if Given Number is Odd or Even using Switch Case in Java Script
  • Write a program to Check if Given Number is Odd or Even using Switch Case in C#

Explanation:

ogic to Check if a Number is Odd or Even

  1. Input the number from the user.
  2. Divide the number by 2 and check the remainder:
    • If the remainder is 0, the number is even.
    • Otherwise, the number is odd.
  3. Output whether the number is odd or even.

Program to Check if Given Number is Odd or Even using Switch Case

#include <stdio.h>

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

    switch (num % 2) {
        case 0:
            printf("The number is even.\n");
            break;
        case 1:
            printf("The number is odd.\n");
            break;
        default:
            printf("Invalid input.\n");
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;

    switch (num % 2) {
        case 0:
            cout << "The number is even." << endl;
            break;
        case 1:
            cout << "The number is odd." << endl;
            break;
        default:
            cout << "Invalid input." << endl;
    }

    return 0;
}

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

def even():
    print("The number is even.")

def odd():
    print("The number is odd.")

switch = {
    0: even,
    1: odd
}

switch[num % 2]()

<?php
$num = readline("Enter a number: ");

switch ($num % 2) {
    case 0:
        echo "The number is even.\n";
        break;
    case 1:
        echo "The number is odd.\n";
        break;
    default:
        echo "Invalid input.\n";
}
?>

import java.util.Scanner;

public class OddEvenSwitch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        switch (num % 2) {
            case 0:
                System.out.println("The number is even.");
                break;
            case 1:
                System.out.println("The number is odd.");
                break;
            default:
                System.out.println("Invalid input.");
        }

        scanner.close();
    }
}

let num = parseInt(prompt("Enter a number:"));

switch (num % 2) {
    case 0:
        console.log("The number is even.");
        break;
    case 1:
        console.log("The number is odd.");
        break;
    default:
        console.log("Invalid input.");
}

using System;

class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int num = Convert.ToInt32(Console.ReadLine());

        switch (num % 2) {
            case 0:
                Console.WriteLine("The number is even.");
                break;
            case 1:
                Console.WriteLine("The number is odd.");
                break;
            default:
                Console.WriteLine("Invalid input.");
                break;
        }
    }
}

List of All Programs