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 PythonIn 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. 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 : 105Output 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.0Output 3: Subtraction 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 : 48Related Python Examples:1. Python program to add two matrices Write a C program to perform addition, subtraction, multiplication and division of two numbers. Pictorial Presentation: Sample Solution: C Code: Sample Output: Flowchart: 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.
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.
There we will perform these arithmetic operations like Addition, Subtraction, Multiplication, Division, and Modulus. In this article, we solved this problem in five methods: 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 linksC 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. ExplanationIn 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 thingsC++ Program to Perform Arithmetic Operations Java Program to Perform Arithmetic Operations Python Program to Perform Arithmetic Operations |