Find elements

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 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

  • The modular operator uses to find the modulus of the number dividing by 2.
  • Switch statements return either 0 or 1.
  • Then, check the case value with 0 or 1.
  • if the case value is 0, the number will be even and case value 1, the number will be odd.
 

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
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago