Answer to Question #198316 in C++ for fatima

Question #198316
In this problem, you are required to 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 ~)).
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);
1
Expert's answer
2021-05-25T05:24:24-0400
#include<iostream>
using namespace std;
int addition(int n1, int n2){
  for(int i=1;i<=n2;i++)
    n1++;
  return n1;
}
int subtraction(int n1, int n2){
  for(int i=1;i<=n2;i++)
    n1--;
  return n1;
}
int multiplication(int n1, int n2){
  int result=0;
  for(int i=1;i<=n2;i++)
    for(int j=1;j<=n1;j++)
      result++;
  return result;
}
int division(int num, int demon){
  int help=0,result=0;
  while(help<num){
    for(int j=0;j<demon;j++)
      help++;
    if(help<=num)
      result++;
  }
  return result;
}
int reminder(int num, int demon){
  int help=0,result=0;
  while(help<num){
    for(int j=0;j<demon;j++)
      help++;
    if(help<=num)
      result++;
  }
  int ans=subtraction(num,multiplication(result,demon));
  return ans;
}
int main(){
  int value1, value2,result;
  char ch;
  cout<<"Enter your first input";
  cin>>value1;
  cout<<"\n Enter your second input";
  cin>>value2;
  cout<<"Enter operator";
  cin>>ch;
  switch(ch){
    case '+': result=addition(value1,value2);
              break;
    case '-' : result=subtraction(value1, value2);
                break;
    case '*' : result=multiplication(value1, value2);
                break;
    case '/' : result=division(value1, value2);
                break;
    case '%' : result=reminder(value1, value2);
                break;
    default : cout<<"Invalid operator";
  }
  cout<<result;
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog