Printing Even and Odd Numbers using While Loop in C++


This is a C++ program that finds even and odd numbers between 1 and a given ending value. Let's go through the program step-by-step:

  • The program starts by including the necessary header file iostream.
  • The using namespace std statement is used to avoid writing std:: before every standard library function.
  • The main() function is the entry point of the program.
  • Inside main(), the program declares some integer variables i, n, even, and odd.
  • The user is prompted to enter the ending value for the range of numbers to be checked.
  • The program then enters a while loop that checks each number in the range between 1 and the ending value.
  • Inside the while loop, the program checks if the number is even or odd using the modulus operator %.
  • If the number is even, the program prints the number and increments the even variable.
  • If the number is odd, the program prints the number and increments the odd variable.
  • The program then continues checking the next number in the range.
  • Once the first while loop completes, the program prints the total number of even and odd numbers found.

Overall, the program correctly identifies even and odd numbers in the given range and prints them to the console. However, there is some code duplication because the program uses two separate while loops to check even and odd numbers. The program could instead use a single while loop and check both even and odd numbers in one iteration. Additionally, the program assumes that the user will enter a positive integer for the ending value, so it may not work correctly for negative numbers or non-integer inputs. It's a good practice to add error checking to ensure that the program behaves correctly for all possible inputs.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i=1,n,even=0,odd=0;
    cout<<"\nEnter the Ending value:";
    cin>>n;
    cout<<"\nEven numbers:";
    while(i<=n)
    {
        if(i%2==0)
        {
            cout<<"\n"<<i;
            even++;
        }
        i++;

    }
    cout<<"\nOdd numbers:";
    i=1;
    while(i<=n)
    {
        if(i%2==1)
        {
            cout<<"\n"<<i;
            odd++;
        }
        i++;
    }
    cout<<"\nTotal even numbers:"<<even;
    cout<<"\nTOtal odd numbers:"<<odd;
    return 0;
}
To download raw file Click Here

Output

Enter the Ending value:20
Even numbers:
2
4
6
8
10
12
14
16
18
20
Odd numbers:
1
3
5
7
9
11
13
15
17
19
Total even numbers:10
TOtal odd numbers:10

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples