C program to check even or odd number using switch case

Write a C program to enter any number and check whether the given number is even or odd using switch case. How to check even or odd using switch case in C programming. Even or odd program using switch case.

Example:
Input number: 12
Output: Even number

Also view this program using -
C program to even or odd number using if else.
C program to check even or odd number using conditional/ternary operator.
C program to check even or odd using functions.

Required knowledge

Basic C programming, Switch case

Even numbers

Even numbers are the positive integers that are exactly divisible by 2. For example: First 5 even numbers are - 2, 4, 6, 8, 10...

Odd numbers

Odd numbers are the positive integers that are not exactly divisible by 2. For example: First 5 odd numbers are - 1, 3, 5, 7, 9...

Logic to check even or odd

Logic to this program is simple and straightforward till now we know that for check whether a number is even or odd we need to perform modular division by 2 and if the result of modular division is zero then the number is zero. Means in general if num%2 == 0 then the number is even otherwise odd. If you want to learn how to check divisibility of any number just check this post - C program to check divisibility of any number.

Program to check even or odd

/**
 * C program to check Even or Odd number using switch case
 */

#include <stdio.h>

int main()
{
    int num;

    /*
     * Reads a number from user
     */
    printf("Enter any number to check even or odd: ");
    scanf("%d", &num);

    switch(num % 2)
    {
        //If n%2 == 0
        case 0: printf("Number is Even");
                break;

        //Else if n%2 != 0
        case 1: printf("Number is Odd");
                break;
    }

    return 0;
} 


Output
Enter any number to check even or odd: 6

Number is Even

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment