Algorithm
This is an example of basic arithmatic operations.
- Take number1 and number2 as input
- Summation of two numbers, summation = number1 + number2
- Subtraction of two numbers, subtraction = number2 - number1
- Multiplication of two numbers, multiplication = number1 * number2;
- Division of two numbers, division = number2 / number1; It's return type needs to be float.
Code Examples
#1 Simple Arithmetic Code Example in One Code
Code -
C Programming
#include <stdio.h>
int summation (number1, number2) {
printf("\nSummation = ");
return number1 + number2;
}
int subtraction (number1, number2) {
printf("\nSubtraction = ");
return number2 - number1;
}
int multiplication (number1, number2) {
printf("\nMultiplication = ");
return number1 * number2;
}
float division (number1, number2) {
printf("\nDivision = ");
return number2 / number1;
}
int main(void){
int number1, number2;
printf("Please Enter Number 1: ");
scanf("%d", &number1);
printf("Please Enter Number 2: ");
scanf("%d", &number2);
printf("%d", summation(number1, number2));
printf("%d", subtraction(number1, number2));
printf("%d", multiplication(number1, number2));
printf("%.2f", division(number1, number2));
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
Demonstration
We've made basic functions to demonstrate this arithematic opeations. We've added the four arithematic operations like,
- Summation
- Subtraction
- Multiplication and
- Division
Code is simpel. Just make function for every operation and call them from main().
Like, for summation, we've made a function summation()
, which return type is integer.
int summation (number1, number2) {
printf("\nSummation = ");
return number1 + number2;
}
And same for the next two subtraction()
and multiplication()
.
For division()
, it's recommeded to make the function return type to float, as after division of two numbers, we could definitely get a float output.
float division (number1, number2) {
printf("\nDivision = ");
return number2 / number1;
}
And Print it also as much as data you need after decimal.
like this -
printf("%.2f", division(number1, number2));
We've print the two decimal digit after it.