Write a program to add, subtract, multiply and divide two numbers in c++

Skip to content

Write a program to add, subtract, multiply and divide two numbers in c++

So in this post, we are going to see how we can do the mathematical operations such as Addition, Subtraction, Multiplication & Division in C Programming language.

It’s fairly simple to do mathematical operations in C language, here I will show you a simple example:

#include<stdio.h> int main() { int add, sub, mul; float div; //addition add = 5 + 2; //subtraction sub = 5 - 2; //multiplication mul = 5 * 2; //division div = 5 / (float)2; printf("Add: %d, Sub: %d, Mul: %d, Div: %.2f", add,sub,mul,div); return 0; }

  printf("Add: %d, Sub: %d, Mul: %d, Div: %.2f", add,sub,mul,div);

Now, this is something the values are hardcoded, now let’s write this application totally dynamic so we can get the input values from the user and do the math operations.

Also, we will give multiple options to do select the operation such as “Select 1 for Addition”, “Select 2 for Subtraction” and more.

Let’s write the complete C Program to do the mathematical operation dynamically:

#include <stdio.h> #include <stdlib.h> int main() { int a, b, add, sub, mul, opt, ans; float div; printf(" 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n"); printf("Select any one option (1 to 4): "); scanf("%d", &opt); //check if the option is entered between 1 and 4 if(opt <= 0 || opt > 4) { printf("Please enter values between 1 and 4!"); exit(0); } //get the values of a and b printf("Enter value of a:"); scanf("%d", &a); printf("Enter value of b:"); scanf("%d", &b); //check the option selected, depends on the selection //do the math operation if(opt == 1) { printf("\nYou have selected 'Addition'\n"); ans = a + b; } else if(opt == 2) { printf("\nYou have selected 'Subtraction'\n"); ans = a - b; } else if(opt == 3) { printf("\nYou have selected 'Multiplication'\n"); ans = a * b; } else if(opt == 4) { printf("\nYou have selected 'Division'\n"); div = a / (float)b; } //print the output if(opt != 4){ printf("Answer: %d", ans); }else{ printf("Answer: %.2f", div); } return 0; }

int a, b, add, sub, mul, opt, ans;

printf(" 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n");

printf("Select any one option (1 to 4): ");

//check if the option is entered between 1 and 4

printf("Please enter values between 1 and 4!");

//get the values of a and b

printf("Enter value of a:");

printf("Enter value of b:");

//check the option selected, depends on the selection

printf("\nYou have selected 'Addition'\n");

printf("\nYou have selected 'Subtraction'\n");

printf("\nYou have selected 'Multiplication'\n");

printf("\nYou have selected 'Division'\n");

printf("Answer: %d", ans);

printf("Answer: %.2f", div);

The output of the above program will be something like this:

1. Addition 2. Subtraction 3. Multiplication 4. Division Select any one option (1 to 4): 3 Enter value of a:6 Enter value of b:4 You have selected 'Multiplication' Answer: 24 -------------------------------- Process exited after 4.695 seconds with return value 0 Press any key to continue . . .

Select any one option (1 to 4): 3

You have selected 'Multiplication'

--------------------------------

Process exited after 4.695 seconds with return value 0

Press any key to continue . . .

Just go through the program line by line to understand it.

Enjoy programming!