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

• Write a program in c for 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

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.

```/*
* C Program for Addition, Subtraction, Multiplication, Division
* and Modulus of two numbers
*/
#include <stdio.h>
#include <conio.h>

int main(){
/* Variable declation */
int firstNumber, secondNumber;
int sum, difference, product, modulo;
float quotient;

/* Taking input from user and storing it in firstNumber and secondNumber */
printf("Enter First Number: ");
scanf("%d", &amp;firstNumber);
printf("Enter Second Number: ");
scanf("%d", &amp;secondNumber);

sum = firstNumber + secondNumber;
/* Subtracting two numbers */
difference = firstNumber - secondNumber;
/* Multiplying two numbers*/
product = firstNumber * secondNumber;
/* Dividing two numbers by typecasting one operand to float*/
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);

getch();
return 0;
}
```

Program 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.

```/*
* C Program to Add, Subtract, multiply, divide and modulo two numbers
* using function
*/
#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;

/* Taking input from user and storing it in firstNumber and secondNumber */
printf("Enter First Number: ");
scanf("%d", &firstNumber);
printf("Enter Second Number: ");
scanf("%d", &secondNumber);

sum = getSum(firstNumber, secondNumber);
/* Subtracting two numbers */
difference = getDifference(firstNumber, secondNumber);
/* Multiplying two numbers*/
product = getProduct(firstNumber, secondNumber);
/* Dividing two numbers */
quotient = getQuotient(firstNumber, secondNumber);
/* Remainder when firstNumber is divided by 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);

getch();
return 0;
}

/*
* Function to add two numbers
*/
int getSum(int num1, int num2){
int sum;
sum = num1 + num2;
return sum;
}

/*
* Function to subtract two numbers
*/
int getDifference(int num1, int num2){
int difference;
difference = num1 - num2;
return difference;
}

/*
* Function to multiply two numbers
*/
int getProduct(int num1, int num2){
int product;
product = num1 * num2;
return product;
}

/*
* Function to divide two numbers
*/
float getQuotient(int num1, int num2){
float quotient;
quotient = (float)num1 / num2;
return quotient;
}

/*
* Function to return remainder when one
* number is divided by other number
*/
int getModulo(int num1, int num2){
int modulo;
modulo = num1 % num2;
return modulo;
}
```

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
{"ft_lang_label":"__label__en","ft_lang_prob":0.6009324,"math_prob":0.9949309,"size":4816,"snap":"2019-26-2019-30","text_gpt3_token_len":1150,"char_repetition_ratio":0.17934331,"word_repetition_ratio":0.24328147,"special_character_ratio":0.2707641,"punctuation_ratio":0.19016817,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9994686,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-06-18T19:02:18Z\",\"WARC-Record-ID\":\"<urn:uuid:7950f8f9-3a7b-417c-a9a1-2aa4f1085b92>\",\"Content-Length\":\"81706\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5adb2827-676f-49c0-8f26-f213a8ce5d2e>\",\"WARC-Concurrent-To\":\"<urn:uuid:097d46e0-de4c-49a7-84db-14838dcbcf00>\",\"WARC-IP-Address\":\"172.217.7.179\",\"WARC-Target-URI\":\"http://www.techcrashcourse.com/2014/10/c-program-addition-subtraction-multiplication-division-modulus.html\",\"WARC-Payload-Digest\":\"sha1:VQ3CNFGKAIF3LEATLMJIGV75CIK5HNTW\",\"WARC-Block-Digest\":\"sha1:O6TPMQJ5P37T4IIHZSXJ3SD36BAQTEMD\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-26/CC-MAIN-2019-26_segments_1560627998813.71_warc_CC-MAIN-20190618183446-20190618205446-00456.warc.gz\"}"}