In this tutorial, we will discuss a concept of the Display even and odd numbers without if statement in C++ programming language
In this post, we are going to learn how to display even and odd numbers without using if statement in C++ programming language.
First, we must understand how to identify an 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 as 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 display odd and even numbers without if statements in C++ programming language
There are three programs given below
Program 1
The program allows the user to enter a maximum number for display odd and even numbers using for loop in C++
It will display the even and odd numbers without using if statements
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num; cout<<"Enter the maximum number\n"; cin>>num; cout<<"Even numbers are :"; //print even numbers using for loop without if statements for(i=2; i<=num; i+=2){ cout<<i<<" "; } cout<<"\nOdd numbers are :"; //print odd numbers using for loop without if statements for(i=1; i<=num; i+=2){ cout<<i<<" "; } getch(); return 0; }
When the above code is executed, it produces the following results
Enter the maximum number 25 Even numbers are : 2 4 6 8 10 12 14 16 18 20 22 24 Odd numbers are : 1 3 5 7 9 11 13 15 17 19 21 23 25
Program 2
The program allows the user to enter the maximum number for print odd and even numbers using while loop in C++ language
It will print the even and odd numbers without using if statements
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num; cout<<"Enter the maximum number\n"; cin>>num; cout<<"Even numbers are :"; //print even numbers using for loop without if statements i=2; while(i<=num){ cout<<i<<" "; i+=2; } cout<<"\nOdd numbers are :"; //print odd numbers using for loop without if statements i=1; while(i<=num){ cout<<i<<" "; i+=2; } 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 Od numbers are: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
Program 3
The program allows the user to enter the maximum number for print odd and even numbers using the do-while loop in C++ language
Then, it will print the even and odd numbers without using if statements in C++ language
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num; cout<<"Enter the maximum number\n"; cin>>num; cout<<"Even numbers are :"; //print even numbers using for loop without if statements i=2; do{ cout<<i<<" "; i+=2; }while(i<=num); cout<<"\nOdd numbers are :"; //print odd numbers using for loop without if statements i=1; do{ cout<<i<<" "; i+=2; }while(i<=num); getch(); return 0; }
When the above code is executed, it produces the following results
Enter the maximum number 40 Even numbers are : 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 Odd numbers are: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Similar post
Display even and odd numbers without if statement in Java
Display even and odd numbers without if statement in Python
Display even and odd numbers without if statement in C
C program to find the largest among three numbers using function
Python code to find the largest of three numbers using the function
C++ code to find the largest of three numbers using the function
Java code to find the largest of three numbers
C code to find the largest of three numbers
C++ code to find the largest of three numbers
Python code to find the largest of three numbers
Java program to find the middle of three number using Method
C code to find the middle of three number using the function
C++ code to find the middle of three number using the function
Python code to find the middle of three number using the function
Java code to find middle of three numbers
C code to find middle of three numbers
C++ code to find middle of three numbers
Python code to find middle of three numbers
Java code to find largest of three numbers in an array
Java code to find smallest of three numbers in an array
C code to find the middle of three number using the function
C++ code to find the middle of three number using the function
Suggested for you
Nested if statement in C++ language
Data type in C++ language
The operator in Python language
If statement in Python language
Data types and Variable in Python
Datatype and variables in Java programming language
If statements in Java language
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…