Display even and odd numbers without if statement in C
- Home
- Find elements
- Display even and odd numbers without if statement in C
- On
- By
- 0 Comment
- Categories: Find elements
Display even and odd numbers without if statement in C
Display even and odd numbers without if statement in C
In this tutorial, we will discuss the concept of Display even and odd numbers without if statement in C language
In this post, we are going to learn how to display even and odd numbers without if statement in C language.
First, we must understand how to identify odd and even numbers
What is Even or Odd
When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10
and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers – eg – 1,3,5,7,9
In my previous post, I have explained the various approaches to print even and odd numbers using if statements in C language
here, we will discuss how to print odd and even numbers without if statements in C programming language
There are three programs given below
Print even and odd numbers using for loop
Program 1
The program allows the user to enter the maximum number for print odd and even numbers using for loop in C language
Then, it will display the even and odd numbers without using if statements in C language
#include <stdio.h> #include <stdlib.h> int main() { int i,num; printf("Enter the maximum number\n"); scanf("%d",&num);//reads input from user printf("Even numbers are :"); //print even numbers using for loop without if statements for(i=2; i<=num; i+=2){ printf("%d ",i); } printf("\nOdd numbers are :"); //print odd numbers using for loop without if statements for(i=1; i<=num; i+=2){ printf("%d ",i); } getch(); return 0; }
When the above code is executed, it produces the following results
Enter the maximum number 30 Even numbers are : 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 Odd numbers are : 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
Print even and odd numbers using while loop
Program 2
The program allows the user to enter the maximum number for display odd and even numbers using while loop in C language
Then, it will display the even and odd numbers without using if statements in C language
#include <stdio.h> #include <stdlib.h> int main() { int i,num; printf("Enter the maximum number\n"); scanf("%d",&num);//reads maximum value from user printf("Even numbers are :"); //print even numbers using for loop without if statements i=2; while(i<=num){ printf("%d ",i); i+=2; } printf("\nOdd numbers are :"); //print odd numbers using for loop without if statements i=1; while(i<=num){ printf("%d ",i); i+=2; } getch(); return 0; }
When the above code is executed, it produces the following results
Enter the maximum number 20 Even numbers are :2 4 6 8 10 12 14 16 18 20 Odd numbers are : 1 3 5 7 11 13 15 17 19
Print even and odd numbers using the do-while loop
Program 3
The program allows the user to enter the maximum number for display odd and even numbers using do- while loop in C language
Then, it will display the even and odd numbers without using if statements in C language
#include <stdio.h> #include <stdlib.h> int main() { int i,num; printf("Enter the maximum number\n"); scanf("%d",&num);//reads maximum value from user printf("Even numbers are :"); //print even numbers using for loop without if statements i=2; do{ printf("%d ",i); i+=2; }while(i<=num); printf("\nOdd numbers are :"); //print odd numbers using for loop without if statements i=1; do{ printf("%d ",i); i+=2; }while(i<=num); getch(); return 0; }
When the above code is executed, it produces the following results
Enter the maximum number 30 Even numbers are : 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 Odd numbers are : 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
Similar post
Print even and odd numbers without if statement in C++
Print even and odd numbers without if statement in Java
Print even and odd numbers without if statement in Python
Suggested for you