Find odd or even number using switch statements in C
- Home
- Check value
- Find odd or even number using switch statements in C
- On
- By
- 0 Comment
- Categories: Check value, 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
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
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