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:
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.
#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
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
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions