Codeforcoding

C++ program to display all odd or even numbers 1 to n with label

C++ program to display all odd or even numbers from 1 to n with label

In this article, we will discuss the concept of C++ program to display all odd or even numbers from 1 to n with label using loops

In this post, we will learn how to find and display odd and even numbers from 1 to n with label using the loops.

Here, we will use for loop, while loop and do-while loop to find and display odd and even numbers from 1 to n in C++ programming language.

C++ program to display all odd or even numbers

Using for loop

This program allows the user to enter the maximum numbers. It finds and displays even and odd numbers with label 1 to entered number using for loop.

program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,i;  //variable declaration
    cout<<"Enter the random number \n";
    cin>>num;//takes input from user(maximum number)

    cout<<"Display all odd and even number till "<<num;

cout<<"\n\n";

for(i=1; i<=num; i++){
    if(i%2==0){
        cout<<i;
                cout<<" Is Even";
        cout<<"\t";
    }

    else{
        cout<<i;
                cout<<" Is Odd\t";
    }

}
getch();
    return 0;
}

When the above code is executed, it produces the following results

C++ program to find the number is odd or even with label using loops
Display the number – odd or even with label using loops

 

Using while loop

Program 2

This program allows the user to enter the maximum numbers. It finds and displays even and odd numbers with label 1 to entered number using while loop.

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,i;//variable declaration
    cout<<"Enter the random number \n";
    cin>>num;//Takes input from user(maximum number)

    cout<<"Display all odd and even number till "<<num;

cout<<"\n\n";

i=1;
while(i<=num){
    if(i%2==0){
        cout<<i;
                cout<<" Is Even";
        cout<<"\t";
    }

    else{
        cout<<i;
                cout<<" Is Odd\t";
    }
     i++;

}
getch();
    return 0;
}


When the above code is executed, it produces the following results

Display the number – odd or even with label using loops

 

Using do-while loop

Program 3

This program allows the user to enter the maximum numbers. It finds and displays even and odd numbers with label 1 to entered number using the do-while loop.

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,i;//variable declaration
    cout<<"Enter the random number \n";
    cin>>num;//Takes input from user

    cout<<"Display all odd and even number till "<<num;

cout<<"\n\n";

i=1;
do{
    if(i%2==0){
        cout<<i;
                cout<<" Is Even";
        cout<<"\t";
    }

    else{
        cout<<i;
                cout<<" Is Odd\t";
    }
     i++;

}while(i<=num);
getch();
    return 0;
}


When the above code is executed, it produces the following results

Display the number – odd or even with label using loops

Similar post

C program to find the number is odd or even with label using loops

Java program to find the number is odd or even with label using loops

Python program to find the number is odd or even with label using loops

Java program to calculate average of  odd or even numbers

C program to calculate average of  odd or even numbers

C++ program to calculate average of  odd or even numbers

Java program to calculate sum of  odd or even numbers

C program to calculate sum of  odd or even numbers of an array

C++ program to calculate sum of  odd or even numbers in an array

Python program to calculate sum of  odd or even numbers in a list

C++ program to find odd or even number using switch statements

C  program to find odd or even 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

 

 

Suggested for you

C++ language for loop

C++ language while loop

C++ language do-while loop

C++ language data type

C++  language variable

C++ language operator

 

 

C program to display all odd or even numbers 1 to n with label
Java program to display all odd or even numbers 1 to n with label
Exit mobile version