Cpp program to display all even or odd numbers from 1 to n
- Home
- Find elements
- Cpp program to display all even or odd numbers from 1 to n
- On
- By
- 0 Comment
- Categories: Find elements
Cpp program to display all even or odd numbers from 1 to n
Cpp program to display all even or odd numbers from 1 to n
In this tutorial, we will discuss the Cpp program to display all even or odd numbers from 1 to n
In this program, we are going to learn about how to find odd or even number from 1 to given number using the loops in the C++ language
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 side when it is divided by 2 and balance becomes 1, they are called odd numbers or uneven numbers
In my previous post, I have explained the various approaches to check whether the number is even or odd in C++ language
Once this program received the number, it will check the given number either odd or even number until the given range.
After receiving the input from the user, it is stored in the variable of num and then program divides the value of num by 2 and displays the output
Print all even number using for loop
In this program, we display all even numbers from 1 to n using for loop
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num; cout<<"Print all even numbers until\n"; cin>>num;//reads input from the user for the maximum value cout<<"Even numbers from 1 to"<<num<< are\n"; for(i=1; i<=num; i++){//loop for iterate from 1 to maximum if(i%2==0)//using modular operator for finding even numbers { cout<<i<<"\n";//print even number } } getch(); return 0; }
When the above code is executed, it produces the following results
Print all even number till 50 Even number from 1 to 50 are 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Print all odd number using for loop
In this program, we display all odd numbers from 1 to n using for loop
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num; cout<<"Print all odd numbers until\n"; cin>>num; //reads input from the user for the maximum value cout<<"Odd numbers from 1 to"<<num<< are\n"; for(i=1; i<=num; i++){//loop for iterate from 1 to maximum if(i%2==1)//using modular operator for finding odd numbers { cout<<i<<"\n"; } } getch(); return 0; }
When the above code is executed, it produces the following results
Print all odd number till 50 Odd number from 1 to 50 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Print all even number using while loop
In this program, we display all even numbers from 1 to n using while loop
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num; cout<<"Print all even number until\n"; cin>>num; //reads input from the user for the maximum value cout<<"Even number from 1 to"<<num<< are\n"; i=1; while(i<=num){//loop for iterate from 1 to maximum if(i%2==0) {//using modular operator for finding even numbers cout<<i<<"\n"; } i++; } getch(); return 0; }
When the above code is executed, it produces the following results
Print all even number till 50 Even number from 1 to 50 are 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Print all odd number using while loop
In this program, we display all odd numbers from 1 to n using while loop
Program 4
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num; cout<<"Print all odd number until\n"; cin>>num; //reads input from the user for the maximum value cout<<"Odd number from 1 to"<<num<< are\n"; i=1; while(i<=num){//loop for iterate from 1 to maximum if(i%2==1) {//using modular operator for finding odd numbers cout<<i<<"\n"; } i++; } getch(); return 0; }
When the above code is executed, it produces the following results
Print all odd number till 50 Odd number from 1 to 50 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Print all even number using do while loop
In this program, we display all even numbers from 1 to n using do-while loop
Program 5
#include <iostream> #include <conio.h> using namespace std; int main() { int num,i; cout<<"Print all even number until\n"; cin>>num; //reads input from the user for the maximum value cout<<"Even number from 1 to "<<num<<" are\n"; i=1; do{//loop for iterate from 1 to maximum if(i%2==0) {//using modular operator for finding even numbers cout<<i<<"\n"; } i++; }while(i<=num); getch(); return 0; }
When the above code is executed, it produces the following results
Print all even number till 50 Even number from 1 to 50 are 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Print all odd number using do while loop
In this program, we display all odd numbers from 1 to n using do-while loop
Program 6
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num; cout<<"Print all odd number until\n"; cin>>num; //reads input from the user for the maximum value cout<<"Odd number from 1 to"<<num<< "are\n"; i=1; do{//loop for iterate from 1 to maximum if(i%2==1) {//using modular operator for finding odd numbers cout<<i<<"\n"; } i++; } while(i<=num); getch(); return 0; }
When the above code is executed, it produces the following results
Print all odd number till 50 Odd number from 1 to 50 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Suggested for you
Similar post
C program to find a number is even or odd using the function
C program to separate Odd and Even numbers from an array
C program to Electricity bill calculation using the function
C program to display all even and odd numbers from 1 to n
C program display odd and even numbers without if statements
C program to calculate the sum of odd and even numbers
C program to find whether a number is even or odd
C++ program to find a number is even or odd using the function
C++ program to separate Odd and Even numbers from an array
C++ program to display all even and odd numbers from 1 to n
C++ program calculate Average of odd and even numbers in an array
C++ program to calculate the sum of odd and even numbers
C++ program to find whether a number is even or odd
Java program to find a number is even or odd using the method
Java program to separate Odd and Even numbers from an array
Java program to display all even and odd numbers from 1 to n
Java program display odd and even numbers without if statements
Java program to calculate the sum of odd and even numbers
Java program to find whether a number is even or odd