C Program To Perform Addition, Subtraction, Multiplication, Division and Modulus of Two Numbers

There are five fundamental arithmetic operators supported by C language, which are addition(+), subtraction(-), multiplication(*), division(/) and modulus(%) of two numbers. All arithmetic operators compute the result of specific arithmetic operation and returns its result.

Arithmetic Operators in C

Operator Description Syntax Example
+ Adds two numbers a + b 15 + 5 = 20
- Subtracts two numbers a - b 15 - 5 = 10
* Multiplies two numbers a * b 15 * 5 = 75
/ Divides numerator by denominator a / b 15 / 5 = 3
% Returns remainder after an integer division a % b 15 % 5 = 0

Advantages of the Arithmetic Operations Program in C

  • Introduction to Mathematical Expressions : Writing this program introduces you to the concept of mathematical expressions in C programming. You will learn how to construct and evaluate expressions involving arithmetic operations, which is essential for more complex programming tasks.

  • Building Block for Scientific Calculations : The Arithmetic Operations Program serves as a building block for programs that require scientific calculations involving arithmetic operations. It lays the foundation for understanding and implementing algorithms that involve arithmetic calculations.

  • Understanding Basic Arithmetic : This program provides a practical application for understanding addition, subtraction, multiplication, division, and modulus operations. It demonstrates how these operations can be implemented in a computer program, enhancing your understanding of basic arithmetic concepts.

Tips for Writing the Arithmetic Operations Program in C

  • Use Parentheses to Clarify Expressions : Use parentheses to clarify the order of operations in complex expressions. This helps ensure that the operations are performed in the intended sequence.

  • Handle Division by Zero : Check for division by zero before performing the division operation. Display an error message or handle the exception appropriately to prevent program crashes.

  • Format Output Appropriately : Format the output of the program to display the results of the arithmetic operations clearly. Use appropriate formatting options to ensure that the results are easy to read and understand.

C program for addition, subtraction, multiplication, division and modulus of two numbers

This program performs basic binary arithmetic operation on two integer operands like addition, subtraction, division and modulus and prints result in screen. It first takes two integers as input from user using scanf function and stores them in 'firstNumber' and 'secondNumber' int variables and performs addition, subtraction, multiplication, division and modulus operations and stores results in 'sum', 'difference' , 'product', 'quotient' and 'modulo' variables respectively. Next, it prints the result on screen using printf function.

#include <stdio.h>

int main(){
    /* Variable declation */
    int firstNumber, secondNumber;
    int sum, difference, product, modulo;
    float quotient;
    
    printf("Enter First Number: ");
    scanf("%d", &amp;firstNumber);
    printf("Enter Second Number: ");
    scanf("%d", &amp;secondNumber);
    
    /* Adding two numbers */
    sum = firstNumber + secondNumber;
    /* Subtracting two numbers */
    difference = firstNumber - secondNumber;
    /* Multiplying two numbers*/
    product = firstNumber * secondNumber;
    /* Dividing two numbers */
    quotient = (float)firstNumber / secondNumber;
    /* returns remainder of after an integer division */
    modulo = firstNumber % secondNumber;
    
    printf("\nSum = %d", sum);
    printf("\nDifference  = %d", difference);
    printf("\nMultiplication = %d", product);
    printf("\nDivision = %.3f", quotient);
    printf("\nRemainder = %d", modulo);
    
    return 0;
}
Output
Enter First Number: 25
Enter Second Number: 4

Sum = 29
Difference  = 21
Multiplication = 100
Division = 6.250
Remainder = 1

Points to Remember
  • If both operands are integers, then the output is also integer and the compiler neglects the term after decimal point.
  • Integer/Float = Float and Float/Integer = Float because of explicit type conversion.
  • Multiplication, division and modulus operator have highest priority than addition and subtraction operator.

C program for addition, subtraction, multiplication, division and modulus of two numbers using a function

This program uses five user defined functions 'getSum', 'getDifference', 'getProduct', 'getQuotient' and 'getModulo' to perform addition, subtraction, multiplication, division and modulus of two numbers.

#include <stdio.h>
 
int getSum(int num1, int num2);
int getDifference(int num1, int num2);
int getProduct(int num1, int num2);
float getQuotient(int num1, int num2);
int getModulo(int num1, int num2);
 
int main(){
    /* Variable declation */
    int firstNumber, secondNumber;
    int sum, difference, product, modulo;
    float quotient;
   
    printf("Enter First Number: ");
    scanf("%d", &firstNumber);
    printf("Enter Second Number: ");
    scanf("%d", &secondNumber);
   
    sum = getSum(firstNumber, secondNumber);
    difference = getDifference(firstNumber, secondNumber);
    product = getProduct(firstNumber, secondNumber);
    quotient = getQuotient(firstNumber, secondNumber);
    modulo = getModulo(firstNumber, secondNumber);
   
    printf("\nSum = %d", sum);
    printf("\nDifference  = %d", difference);
    printf("\nMultiplication = %d", product);
    printf("\nDivision = %.3f", quotient);
    printf("\nRemainder = %d", modulo);
   
    return 0;
}
 
int getSum(int num1, int num2){
    int sum;
    sum = num1 + num2;
    return sum;
}
 
int getDifference(int num1, int num2){
    int difference;
    difference = num1 - num2;
    return difference;
}
 
int getProduct(int num1, int num2){
    int product;
    product = num1 * num2;
    return product;
}
 
float getQuotient(int num1, int num2){
    float quotient;
    quotient = (float)num1 / num2;
    return quotient;
}
 
int getModulo(int num1, int num2){
    int modulo;
    modulo = num1 % num2;
    return modulo;
}
Output
Enter First Number: 25
Enter Second Number: 4

Sum = 29
Difference  = 21
Multiplication = 100
Division = 6.250
Remainder = 1

Important Points to Remember

  • Data Type Compatibility : Ensure that the data types of the variables used for arithmetic operations are compatible. For example, use floating-point data types for division operations to retain decimal values.

  • Error Handling : Implement error-handling mechanisms to gracefully handle any errors that may occur during the arithmetic operations. Display informative messages to guide the user on how to correct the issue.

  • Avoid Integer Division Truncation : Be mindful of integer division truncation, where the result of an integer division operation is truncated to an integer value. Use floating-point data types or type casting to retain decimal values if needed.
Related Topics
C program to check whether an alphabet is a vowel or consonant
C program for Input/Output of Integer, Character and Floating point numbers
C program to check odd or even numbers
C program to read and print string
C program to check year is leap year or not
C program to check a number is palindrome or not
C program to check if two strings are anagram
List of all C programs