0% found this document useful (0 votes)
17 views3 pages

Loop

The program allows a user to perform basic math operations like addition, subtraction, multiplication and division. It prompts the user to select an operator and then enter two numbers. It performs the calculation and displays the result. The user is asked if they want to try another calculation, and the program will continue running until the user enters 'n' or 'N' to quit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Loop

The program allows a user to perform basic math operations like addition, subtraction, multiplication and division. It prompts the user to select an operator and then enter two numbers. It performs the calculation and displays the result. The user is asked if they want to try another calculation, and the program will continue running until the user enters 'n' or 'N' to quit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

using namespace std;

int main()
{
char op,ch;
int num1, num2, answer, counter = 0;
while (counter <=0)
{
cout <<"+ for addition"<<endl;
cout <<"- for subtraction"<<endl;
cout <<"* for multiplication"<<endl;
cout <<"/ for division" <<endl;
cin>>op;

if (op == '+')
{
cout<<"Enter 1st number: ";
cin>>num1;
cout<<"Enter 2nd number: ";
cin>> num2;
answer = num1 + num2;
cout<<"The answer is: "<<answer<<endl;
cout<<"Do you want to try again? (y/n): ";
cin>>ch;
if (ch=='y' || ch == 'Y')
{
counter = 0;
}
else
{
cout<<"Thank you";
counter++;
break;
}
}

else if (op == '-')


{
cout<<"Enter 1st number: ";
cin>>num1;
cout<<"Enter 2nd number: ";
cin>> num2;
answer = num1 - num2;
cout<<"The answer is: "<<answer<<endl;
cout<<"Do you want to try again? (y/n): ";
cin>>ch;
if (ch=='y' || ch == 'Y')
{
counter = 0;
}
else
{
cout<<"Thank you";
counter++;
break;
}
}
else if (op == '*')
{
cout<<"Enter 1st number: ";
cin>>num1;
cout<<"Enter 2nd number: ";
cin>> num2;
answer = num1 * num2;
cout<<"The answer is: "<<answer<<endl;
cout<<"Do you want to try again? (y/n): ";
cin>>ch;
if (ch=='y' || ch == 'Y')
{
counter = 0;
}
else
{
cout<<"Thank you";
counter++;
break;
}
}
else if (op == '/')
{
cout<<"Enter 1st number: ";
cin>>num1;
cout<<"Enter 2nd number: ";
cin>> num2;
answer = num1 / num2;
cout<<"The answer is: "<<answer<<endl;
cout<<"Do you want to try again? (y/n): ";
cin>>ch;
if (ch=='y' || ch == 'Y')
{
counter = 0;
}
else
{
cout<<"Thank you";
counter++;
break;
}

}else
{
cout <<"Wrong Choice"<<endl;
cout<<"Do you want to try again? (y/n): ";
cin>>ch;
if (ch=='y' || ch == 'Y')
{
counter = 0;
}
else
{
cout<<"Thank you";
counter++;
break;
}
}
}

return 0;
}

You might also like