Tag Archives: #WSQ05

Homework 5: Calculator

This program will make some basic operations. It’s just like the program of the first homework, but using functions.

We have to create 5 functions: sum, subtraction, multiplication, division and remainder. Each function must read two integer parameters (the same for each one) given for the user. Also, we have to declare another variable which keep the result of each function. It’s very intuitive to make this program, we only have to use the corresponding mathematical operator (and, of course, we use module ‘%’ for function remainder).

Here is the code that I wrote.

#include <iostream>
#include <math.h>
using namespace std;

int sum (int x, int y)
{
  int R;
  R=x+y;
  return R;
}

int dif (int x,int y)
{
  int R;
  R=fabs(x-y);
  return R;
}

int prod (int x, int y)
{
  int R;
  R=x*y;
  return R;
}

int div (int x, int y)
{
  int R;
  R=x/y;
  return R;
}

int mod (int x, int y)
{
  int R;
  R=x%y;
  return R;
}

int main()
{
  int x, y;
  cout << "\nFirst number: ";
  cin >> x;
  cout << "Second number: ";
  cin >> y;

  cout << "\nSum= " << sum(x,y) << endl << "Difference= " << dif(x,y) << endl << "Product= " << prod(x,y) << endl
  << "Division (first/second)= " << div(x,y) << endl << "Remainder of division= " << mod(x,y) << "\n\n";
  return 0;
}