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


Example Program:

Here is the simple program for printing positive and negative numbers using While Loop in C++.

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

Basic Programs


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


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes