Negative input detector || do while loop || C++
In this, we are going to see do-while loop, the program is based on do-while loop which, detects the negative number if inputted by user.

The code given below can be used for TURBO C++ Compiler:-

    #include<iostream.h>
#include<conio.h>

void main()
{
    clrscr();
    int num;
    int sum = 0;
    do
    {
        cout << "Enter only non negative number ? Or else your negative number will be caught\n";
        cin >> num;
    }
    while (num>=0);
    cout << "Hey you entered negative number : " << num <<" We have detected it";
    getch();
}
    
    

The code given below can be used for g++/gcc Compiler:-

    #include<iostream>
using namespace std;

int main()
{
    int num;
    int sum = 0;
    do
    {
        cout << "Enter only non negative number ? Or else your negative number will be caught\n";
        cin >> num;
    }
    while (num>=0);
    cout << "Hey you entered negative number : " << num <<" We have detected it";
    return 0;
}
    
    

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post