Codeforcoding

Find odd or even number using switch statements in C

Find the odd or even number using switch statements in C

In this tutorial, we will discuss the concept of the find the odd or even number using switch statements in C language.

In this post, we are going to learn how to find odd or even number using switch statements in C programming language

 

when you devide a number by two and if the balance is zero, it is an even number

when you  divided a number by two  and if the balance is one, it is an odd number

Find the odd or even number
Find odd and even number

Example of even number 2,4,6,8,…..

Example of odd number 1,3,5,7,…..

 

Here, we will use a modular operator to display odd or even number of the given number

if n%2==0,  n is a even number

if n%2==1,  n is a odd number

 

 

Program to Find the Even number – Standard method

The program will check and display the  even numbers from the given number using switch statements in C

Program 1

//C program to check whether number is EVEN using switch
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1=450;
    //int num2=561;
switch(num1%2){//this will return either 0 or 1
case 0:
    printf("%d is a even number",num1);
    break;
case 1:
    printf("%d is a odd number",num2);
}
getch();
    return 0;
}

When the above code is executed, it produces the following results

450 is an Even number

 

Program to Find the odd number – Standard method

The program will check and display the  odd numbers from the given number using switch statements

Program 2

//C program to check whether number is ODD using switch
#include <stdio.h>
#include <stdlib.h>

int main()
{
    //int num1=450;
    int num2=561;
switch(num2%2){//this will return either 0 or 1
case 0:
    printf("%d is an even number",num1);
    break;
case 1:
    printf("%d is an odd number",num2);
}
getch();
    return 0;
}

When the above code is executed, it produces the following results

561 is an odd number

Program to Find the odd or Even number – Entered by user

This program allows the user to enter a number and then, the program will check and display the  odd or even numbers from the given number using switch statements in C language

Program 3

//C program to check whether number is EVEN or ODD using switch
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    printf("Enter the number\n");
    scanf("%d",&num); //this will return either 0 or 1

    switch(num%2){
case 0:
    printf("%d is a even number",num);
    break;
case 1:
    printf("%d is a odd number",num);
}
getch();
    return 0;
}

When the above code is executed, it produces the following results

Case 1

Enter the number
35
35 is a odd number

 

Case 2

Enter the number
50
50 is a even number

Approach


Suggested for you

Datatype in C language

Variable in C language

Operator in C language

input output function in C language

Switch statements in C language

 

 

Similar post

C++ program to discover even or odd  number using switch statements

Java  program to discover even or odd number using switch statements

Java program to check odd and even using recursion

C program to check odd and even using recursion

C++ program to check odd and even using recursion

Python program to check odd and even using recursion

Python program to check odd and even using function

Java program to check odd and even using Method

C program to check odd and even using function

 

 

 

Java program to find odd or even number using switch statements
C++ program to find the odd or even number using switch statements
Exit mobile version