C++ program that adds, subtracts, multiplies, or divides two integers
###### Posted by Samath
Last Updated: November 04, 2014

Create a C++ program that adds, subtracts, multiplies, or divides two integers. The program will need to get a letter (A for addition, S for subtraction, M for multiplication, or D for division) and two integers from the user. If the user enters an invalid letter, the program should not ask the user for the two integers. Instead, it should display an appropriate error message before the program ends. If the letter is A (or a), the program should calculate and display the sum of both integers. If the letter is S (or s), the program should display the difference between both integers. When calculating the difference, always subtract the smaller number from the larger one. If the letter is M (or m), the program should display the product of both integers. If the letter is D (or d), the program should divide both integers, always dividing the larger number by the smaller one. (Utilize a switch structure).

Here is the Solution to the problem above:

```#include <iostream>
using namespace std;

{
return a + b;
}
int subtract(int a, int b)
{
if(a>b)
{
return a-b;
}
else
{
return b-a;
}
}
int multiply(int a, int b)
{
return a * b;
}

int divide(int a, int b)
{
if(a>b)
{
return a/b;
}
else
{
return b/a;
}
}

int main(int argc, char *argv[])
{
char op;
int num1;
int num2;
cout<<"Enter (A for addition, S for subtraction, M for multiplication, or D for division): ";
cin>>op;

switch(op)
{
case 'A':
case 'a':
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
break;
case 'S':
case 's':
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<"Result: "<<subtract(num1,num2);
break;
case 'M':
case 'm':
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<"Result: "<<multiply(num1,num2);
break;
case 'D':
case 'd':
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<"Result: "<<divide(num1,num2);
break;
default: cout<<"Invalid Input";
break;
}

return 0;
}
```
###### C++ program that prompts the user to input five decimal numbers, prints the five decimal numbers, converts each decimal number to the nearest integer, adds the five integers and prints the sum and average of the five integers.
Samath | Apr 06, 2022
###### JavaScript program that divides an array into chunks of a specified size
SceDev | Feb 24, 2024
###### C program that receives a positive float number and divides it by two until the result is less than or equal to zero
Samath | Jun 12, 2015
###### C program that multiplies the matrix a by the matrix b and stores the result in the matrix c
Samath | May 04, 2016
###### C Program to Read integers into an array and Reversing them using Pointers
Samath | Jan 02, 2017
###### Java program that prints the sum of the first ten positive integers
Samath | Jan 08, 2021
###### Java program that prints the product of the first ten positive integers
Samath | Jan 08, 2021
###### Java program that reads in three integers and prints "in order" if they are sorted in ascending or descending order, or "not in order" otherwise
Samath | Feb 12, 2021
###### JavaScript program that sort an array of integers in ascending order
SceDev | Feb 17, 2024
###### C program that takes two integers as command line arguments and divide the first argument by the second argument
Samath | May 04, 2016
###### Java program that reads four integers and prints "two pairs" if the input consists of two matching pairs and "not two pairs" otherwise
Samath | Feb 12, 2021