Write a program to accept two numbers and mathematical operators and perform operations accordingly

The arithmetic operations are performed by calculator where we can perform addition, subtraction, multiplication and division. This example shows the basic arithmetic operations i.e.

  • Addition
  • Subtraction
  • Multiplication
  • Division

Let's understand the following example.

Example -

# Store input numbers: num1 = input('Enter first number: ') num2 = input('Enter second number: ') # Add two numbers sum = float(num1) + float(num2) # Subtract two numbers min = float(num1) - float(num2) # Multiply two numbers mul = float(num1) * float(num2) #Divide two numbers div = float(num1) / float(num2) # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) # Display the subtraction print('The subtraction of {0} and {1} is {2}'.format(num1, num2, min)) # Display the multiplication print('The multiplication of {0} and {1} is {2}'.format(num1, num2, mul)) # Display the division print('The division of {0} and {1} is {2}'.format(num1, num2, div))

Output:

Enter first number: 10 Enter second number: 20 The sum of 10 and 20 is 30.0 The subtraction of 10 and 20 is -10.0 The multiplication of 10 and 20 is 200.0 The division of 10 and 20 is 0.5

Explanation -

In the above code, we have taken the user input as an integer using the input() function. Then, we have performed the arithmetical operation on the given numbers and print the result using the print statement. We used the format() method to format the string

Note - To learn more about the format() method, visit our format() method tutorial.

Next TopicPython Area of a Triangle

Write a program to accept two numbers and mathematical operators and perform operations accordingly
For Videos Join Our Youtube Channel: Join Now

  • Send your Feedback to [email protected]
Write a program to accept two numbers and mathematical operators and perform operations accordingly
Write a program to accept two numbers and mathematical operators and perform operations accordingly
Write a program to accept two numbers and mathematical operators and perform operations accordingly

In this tutorial, we will write a Python program to add, subtract, multiply and divide two input numbers.

Program to perform addition, subtraction, multiplication and division on two input numbers in Python

In this program, user is asked to input two numbers and the operator (+ for addition, – for subtraction, * for multiplication and / for division). Based on the input, program computes the result and displays it as output.
To understand this program you should know how to get the input from user and the basics of if..elif..else statement.

# Program published on https://beginnersbook.com # Python program to perform Addition Subtraction Multiplication # and Division of two numbers num1 = int(input("Enter First Number: ")) num2 = int(input("Enter Second Number: ")) print("Enter which operation would you like to perform?") ch = input("Enter any of these char for specific operation +,-,*,/: ") result = 0 if ch == '+': result = num1 + num2 elif ch == '-': result = num1 - num2 elif ch == '*': result = num1 * num2 elif ch == '/': result = num1 / num2 else: print("Input character is not recognized!") print(num1, ch , num2, ":", result)

Output 1: Addition

Enter First Number: 100 Enter Second Number: 5 Enter which operation would you like to perform? Enter any of these char for specific operation +,-,*,/: + 100 + 5 : 105

Output 2: Division

Enter First Number: 20 Enter Second Number: 5 Enter which operation would you like to perform? Enter any of these char for specific operation +,-,*,/: / 20 / 5 : 4.0

Output 3: Subtraction

Enter First Number: 8 Enter Second Number: 7 Enter which operation would you like to perform? Enter any of these char for specific operation +,-,*,/: - 8 - 7 : 1

Output 4: Multiplication

Enter First Number: 6 Enter Second Number: 8 Enter which operation would you like to perform? Enter any of these char for specific operation +,-,*,/: * 6 * 8 : 48

Write a program to accept two numbers and mathematical operators and perform operations accordingly

Related Python Examples:

1. Python program to add two matrices
2. Python program to add two binary numbers
3. Python program to add two numbers
4. Python program to swap two numbers

Last update on August 09 2022 03:30:38 (UTC/GMT +8 hours)

Write a C program to perform addition, subtraction, multiplication and  division of two numbers.

Pictorial Presentation:

Write a program to accept two numbers and mathematical operators and perform operations accordingly

Sample Solution:

C Code:

#include <stdio.h> int main() { int num1, num2; int sum, sub, mult, mod; float div; /* * Read two numbers from user separated by comma */ printf("Input any two numbers separated by comma : "); scanf("%d,%d", &num1, &num2); /* * Performs all arithmetic operations */ sum = num1 + num2; sub = num1 - num2; mult = num1 * num2; div = (float)num1 / num2; mod = num1 % num2; /* * Prints the result of all arithmetic operations */ printf("The sum of the given numbers : %d\n", sum); printf("The difference of the given numbers : %d\n", sub); printf("The product of the given numbers : %d\n", mult); printf("The quotient of the given numbers : %f\n", div); printf("MODULUS = %d\n", mod); return 0; }

Sample Output:

Input any two numbers separated by comma : 10,5 The sum of the given numbers : 15 The difference of the given numbers : 5 The product of the given numbers : 50 The quotient of the given numbers : 2.000000 MODULUS = 0

Flowchart:

Write a program to accept two numbers and mathematical operators and perform operations accordingly

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to calculate the sum of three numbers with getting input in one line separated by a comma.
Next: Write a C  program to find the third angle of a triangle if two angles are given.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

How to increment a pointer address and pointer's value?

First, the ++ operator takes precedence over the * operator, and the () operators take precedence over everything else.

Second, the ++number operator is the same as the number++ operator if you're not assigning them to anything. The difference is number++ returns number and then increments number, and ++number increments first and then returns it.

Third, by increasing the value of a pointer, you're incrementing it by the sizeof its contents, that is you're incrementing it as if you were iterating in an array.

Ref : https://bit.ly/3vMewPt

#include<stdio.h> #include<conio.h> main() { int n,rem,a,b; char op; clrscr(); printf("Enter Operator(+,-,*,/)..."); scanf("%c",&op); printf("Enter numbers..."); scanf("%d%d",&a,&b); switch(op) { case '+' : printf("Result : %d ",a+b);break; case '-' : printf("Result : %d ",a-b);break; case '*' : printf("Result : %d ",a*b);break; case '/' : printf("Result : %d ",a/b);break; default : printf("Invalid operator...."); } getch(); }

In this article, you will learn how to perform all arithmetic operations of two integer numbers in the C programming language.

Example

Enter any two positive integer numbers: 5

7

Addition of       5 + 7 = 12 Subtraction of    5 - 7 = -2 Multiplication of 5 * 7 = 35 Division of       5 / 7 = 0.714286

Modulus of        5 % 7 = 5

You should have the knowledge of the following topics in c programming to understand this program.

  • C Arithmetic operators
  • C Functions
  • C if-else statement
  • C switch case statement
  • C main() function
  • C printf() function

There we will perform these arithmetic operations like Addition, Subtraction, Multiplication, Division, and Modulus.

In this article, we solved this problem in five methods:

// C Program to Perform Arithmetic Operations on Two Numbers #include <stdio.h> int main() { int p, q; int sum, sub, mul, mod; float div; // It will take two integer numbers printf("Enter any two positive integer numbers:\n"); scanf("%d%d", &p, &q); // It will perform all arithmetic operations sum = p + q; sub = p - q; mul = p * q; div = (float)p / q; mod = p % q; // It will print the final output of the program printf("\n"); printf("Addition of %d + %d = %d\n", p, q, sum); printf("Subtraction of %d - %d = %d\n", p, q, sub); printf("Multiplication of %d * %d = %d\n", p, q, mul); printf("Division of %d / %d = %f\n", p, q, div); printf("Modulus of %d %% %d = %d\n", p, q, mod); return 0; }

Run Program

Output

Enter any two positive integer numbers: 5

7

Addition of       5 + 7 = 12 Subtraction of    5 - 7 = -2 Multiplication of 5 * 7 = 35 Division of       5 / 7 = 0.714286

Modulus of        5 % 7 = 5

// C Program to Perform Arithmetic Operations using If else Statement #include <stdio.h> int main() { int p, q, choice; // It will take two integer numbers printf("Enter any two positive integer numbers:\n"); scanf("%d%d", &p, &q); // It will suggest to choose an option to make the operation printf("\nInput your choice to make an operation\n"); printf("\n1 :: for Addition"); printf("\n2 :: for Substraction"); printf("\n3 :: for Multiplication"); printf("\n4 :: for Division"); printf("\n5 :: for Modulus"); printf("\n\nEnter your choice: "); scanf("%d", &choice); // It will perform operation // According to user's choice & print the final output printf("\n"); if (choice == 1) { printf("Addition of %d + %d = %d\n", p, q, p + q); } else if (choice == 2) { printf("Substraction of %d - %d = %d\n", p, q, p - q); } else if (choice == 3) { printf("Multiplication of %d * %d = %d\n", p, q, p * q); } else if (choice == 4) { printf("Division of %d / %d = %f\n", p, q, (float)p / q); } else if (choice == 5) { printf("Modulus of %d %% %d = %d\n", p, q, p % q); } else { printf("\nKindly input correct choice!\n"); } return 0; }

Run Program

Also, visit these links

C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade

C++ Program to Calculate Percentage and Grade

Java Program to Calculate Grade of students

Python Program to Calculate Total Marks Percentage and Grade of a Student

// C Program to Perform Arithmetic Operations using Switch statement #include <stdio.h> int main() { int p, q, choice; // It will take two integer numbers printf("Enter any two positive integer numbers:\n"); scanf("%d%d", &p, &q); // It will suggest to choose an option to make the operation printf("\nInput your choice to make an operation\n"); printf("\n1 :: for Addition"); printf("\n2 :: for Substraction"); printf("\n3 :: for Multiplication"); printf("\n4 :: for Division"); printf("\n5 :: for Modulus"); printf("\n\nEnter your choice: "); scanf("%d", &choice); // It will perform operation // According to user's choice & print the final output printf("\n"); switch (choice) { case 1: printf("Addition of %d + %d = %d\n", p, q, p + q); break; case 2: printf("Substraction of %d - %d = %d\n", p, q, p - q); break; case 3: printf("Multiplication of %d * %d = %d\n", p, q, p * q); break; case 4: printf("Division of %d / %d = %f\n", p, q, (float)p / q); break; case 5: printf("Modulus of %d %% %d = %d\n", p, q, p % q); break; default: printf("\nKindly input correct choice!\n"); break; } return 0; }

Run Program

Output

Enter any two positive integer numbers: 5

7

Input your choice to make an operation

1 :: for Addition 2 :: for Substraction 3 :: for Multiplication 4 :: for Division

5 :: for Modulus

Enter your choice: 1

Addition of 5 + 7 = 12

// C Program to Perform Arithmetic Operations using Functions #include <stdio.h> // @Function to make Addition int Addition(int p, int q) { return p + q; } // @Function to make Subtraction int Subtraction(int p, int q) { return p - q; } // @Function to make Multiplication int Multiplication(int p, int q) { return p * q; } // @Function to make Division float Division(int p, int q) { return (float)p / q; } // @Function to make Modulus int Modulus(int p, int q) { return p % q; } int main() { int p, q; // It will take two integer numbers printf("Enter any two positive integer numbers:\n"); scanf("%d%d", &p, &q); // It will call the all user-defined function // Then, print the final output of the program printf("\n"); printf("Addition of %d + %d = %d\n", p, q, Addition(p, q)); printf("Subtraction of %d - %d = %d\n", p, q, Subtraction(p, q)); printf("Multiplication of %d * %d = %d\n", p, q, Multiplication(p, q)); printf("Division of %d / %d = %f\n", p, q, Division(p, q)); printf("Modulus of %d %% %d = %d\n", p, q, Modulus(p, q)); return 0; }

Run Program

Note: In this program, we used float data type to make the division operation.

Explanation

In this given program, we have taken inputs 5 and 7 from the user then we applied arithmetic operations on these integer numbers.

Then this will be returned the output values of this program.

// C Program to Perform Arithmetic Operations using Pointers #include <stdio.h> int main() { int p, q; int *ptr1, *ptr2; int sum, sub, mul, mod; float div; // It will take two integer numbers printf("Enter any two positive integer numbers:\n"); scanf("%d%d", &p, &q); // To store the address of `p` & `q` ptr1 = &p; ptr2 = &q; // It will perform all arithmetic operations sum = *ptr1 + *ptr2; sub = *ptr1 - *ptr2; mul = *ptr1 * *ptr2; div = (float)*ptr1 / *ptr2; mod = *ptr1 % *ptr2; // It will print the final output of the program printf("\n"); printf("Addition of %d + %d = %d\n", *ptr1, *ptr2, sum); printf("Subtraction of %d - %d = %d\n", *ptr1, *ptr2, sub); printf("Multiplication of %d * %d = %d\n", *ptr1, *ptr2, mul); printf("Division of %d / %d = %f\n", *ptr1, *ptr2, div); printf("Modulus of %d %% %d = %d\n", *ptr1, *ptr2, mod); return 0; }

Run Program

// C Program to Perform all Arithmetic Operations using Pointers #include <stdio.h> int main() { int p, q; int sum, sub, mul, mod; float div; int *x, *y; // These are the pointer variables // It will take two integer numbers printf("Enter any two positive integer numbers:\n"); scanf("%d%d", &p, &q); x = &p; y = &q; // It will perform all arithmetic operations sum = *x + *y; sub = *x - *y; mul = *x * *y; div = (float)*x / *y; mod = *x % *y; // It will print the final output of the program printf("\n"); printf("Addition of %d + %d = %d\n", *x, *y, sum); printf("Subtraction of %d - %d = %d\n", *x, *y, sub); printf("Multiplication of %d * %d = %d\n", *x, *y, mul); printf("Division of %d / %d = %f\n", *x, *y, div); printf("Modulus of %d %% %d = %d\n", *x, *y, mod); return 0; }

Run Program

Output

Enter any two positive integer numbers: 6

8

Addition of       6 + 8 = 14 Subtraction of    6 - 8 = -2 Multiplication of 6 * 8 = 48 Division of       6 / 8 = 0.750000

Modulus of        6 % 8 = 6

You should also read these things

C++ Program to Perform Arithmetic Operations

Java Program to Perform Arithmetic Operations

Python Program to Perform Arithmetic Operations