All Arithmetic Examples in C Programming in one Code

Algorithm


This is an example of basic arithmatic operations.

  1. Take number1 and number2 as input
  2. Summation of two numbers, summation = number1 + number2
  3. Subtraction of two numbers, subtraction = number2 - number1
  4. Multiplication of two numbers, multiplication = number1 * number2;
  5. 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

x
+
cmd
10 40

Output

x
+
cmd
Please Enter Number 1: 10 Please Enter Number 2: 40 Summation = 50 Subtraction = 30 Multiplication = 400 Division = 4.00

Demonstration


We've made basic functions to demonstrate this arithematic opeations. We've added the four arithematic operations like, 

  1. Summation
  2. Subtraction
  3. Multiplication and
  4. 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.