use for/while loop only to implement a calculator using ++ (increment) and -- (decrement) operators only. The operations that your calculator shall perform are limited, i.e. addition, subtraction, multiplication, division, remainder and absolute. Your program shall ask the user about the two input values and an operator, and then use the switch-case structure to run the required operation (addition, subtraction, multiplication, division or absolute (symbol ~)). Your program shall run correctly for positive input values only. Prototype are: int addition(int n1,int n2); int subtraction(int n1,int n2); int multiplication(int n1,int n2); int division(int num,int denom); int remainder(int num,int denom); int absolute(int num);Take only one input for absolute function.All functions inputs can be positive or negative except remainder function, whichaccept only positive values (>0).Take input and display output in main function.write a program for this problem
#include <iostream>
using namespace std;
int main()
{
int one, two, result = 0, choice; // declaring variables
cout << "First Input: "; // taking user input of two numbers
cin >> one;
cout << "Second Input: ";
cin >> two;
cout << "Operation: "; // taking user input of operation
string op;
cin >> op;
if (op.compare("Addition") == 0)
choice = 1;
if (op.compare("Subtraction") == 0)
choice = 2;
if (op.compare("Multiplication") == 0)
choice = 3;
if (op.compare("Division") == 0)
choice = 4;
switch(choice)
{
case 1: // addition case
for(int x=1; x<=two; x++)
one++;
result = one;
break;
case 2: // subtraction case
for(int x=1; x<=two; x++)
one--;
result = one;
break;
case 3: // multiplication case
for(int x=1; x<=two; x++)
for(int y=1; y<=one; y++)
result++;
break;
case 4: // division case
int help = 0;
for(;help < one;)
{
for(int j=0; j<two; j++)
help++;
if(help <= one)
result++;
}
break;
}
cout << "Answer: " << result;
}
Comments
Leave a comment