Printing Positive and Negative Numbers using While Loop in C++


This is a C++ program that finds positive and negative numbers between a given starting and 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, pos, and neg.
  • The user is prompted to enter the starting and ending values for the range of numbers to be checked.
  • The program then enters a while loop that checks each number in the range between the starting and ending values.
  • Inside the while loop, the program checks if the number is negative or positive.
  • If the number is negative, the program prints the number and increments the neg variable.
  • If the number is positive, the program prints the number and increments the pos 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 positive and negative numbers found.

Overall, the program correctly identifies positive and negative 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 positive and negative numbers. The program could instead use a single while loop and check both positive and negative numbers in one iteration. Additionally, the program assumes that the user will enter a range where both positive and negative numbers exist, so it may not work correctly for ranges that contain only positive or negative numbers. 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,n,pos=0,neg=0;
    cout<<"\nEnter the starting value:";
    cin>>i;
    cout<<"\nEnter the Ending value:";
    cin>>n;
    cout<<"\nNegative numbers:";
    while(i<=n)
    {
        if(i<0)
        {
            cout<<"\n"<<i;
            neg++;
        }
        i++;
    }
    cout<<"\nPositive numbers:";
    i=0;
    while(i<=n)
    {
        if(i>0)
        {
            cout<<"\n"<<i;
            pos++;
        }
        i++;
    }
    cout<<"\nTotal number of Positive :"<<pos;
    cout<<"\nTotal number of Negative :"<<neg;
    return 0;
}
To download raw file Click Here

Output

Enter the starting value:1
Enter the Ending value:10
Negative numbers:
Positive numbers:
1
2
3
4
5
6
7
8
9
10
Total number of Positive :10
Total number of Negative :0

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