In this post, I am going to write a very simple C program to do Basic Arthmatics. This is an example C program that is useful for Beginners those who want to know how to do Arithmetic operations in C.
#include <stdio.h>
int main() {
//Declare variables
int number1, number2, addition, subtraction, multiplication, division, modulo;
//get the input from user
printf("Enter number 1:");
scanf("%d", &number1);
printf("Enter number 2:");
scanf("%d", &number2);
//adding two number in C
addition = number1 + number2;
//subtracting two numbers in C
subtraction = number1 - number2;
//multiplying two numbers in C
multiplication = number1 * number2;
//Dividing two numbers in C
division = number1 / number2;
//Get the modulo of two numbers in C
modulo = number1 % number2;
//print the results
printf("\nAddition of %d and %d is equal to %d \n", number1, number2, addition);
printf("Subtraction of %d and %d is equal to %d \n", number1, number2, subtraction);
printf("Multiplication of %d and %d is equal to %d \n", number1, number2, multiplication);
printf("Division of %d and %d is equal to %d \n", number1, number2, division);
printf("Modulo of %d and %d is equal to %d \n", number1, number2, modulo);
return 0;
}Just go through the inline comments in the above program to understand it.
Here is the sample output of the above Arithmetic C program:
Enter number 1:5 Enter number 2:3 Addition of 5 and 3 is equal to 8 Subtraction of 5 and 3 is equal to 2 Multiplication of 5 and 3 is equal to 15 Division of 5 and 3 is equal to 1 Modulo of 5 and 3 is equal to 2 -------------------------------- Process exited after 1.76 seconds with return value 0 Press any key to continue . . .
I believe this post would have helped you to understand how to do Addition, Subtraction, Multiplication, Division, and Module in C language. Basically to do the Arithmetics in C programming.