C Program to Enter Two Numbers and Perform All Arithmetic Operations
All arithmetic operations of two integer numbers in the C programming language.
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 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:
- Using the normal calculation
- Using the if...else...if ladder
- Using the switch case
- Using the functions
- Using the pointers
C Program to Perform Arithmetic Operations on Two Numbers
// 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;
}
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
// 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;
}
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
// 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;
}
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
// 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;
}
Explanation
In this given program, we have taken input 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
// 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;
}
C Program to Perform all Arithmetic Operations using Pointers
// 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;
}
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
C Program to Perform Arithmetic Operations using Command Line Arguments
// C Program to Perform Arithmetic Operations using Command Line Arguments
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
int p, q, result;
int sum, sub, mul, mod;
float div;
if (argc < 4) {
printf("Command-line arguments are missing!\n");
printf("Put inputs as Value1 Value2\n");
return -1;
}
// It will get the values from the command-line arguments
p = atoi(argv[1]);
q = atoi(argv[2]);
// 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;
}
Output
$ gcc main.c -o main
$ ./main 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 also read these things
C++ Program to Perform Arithmetic Operations