0% found this document useful (0 votes)
194 views

C Program To Do Arithmetic

The document describes a C program that takes in two numbers as input and calculates the sum, difference, product, quotient, and modulus of the two numbers. It includes sample input and output. The program first prompts the user to input two integers, then performs the five arithmetic operations on those numbers and prints out the results. To avoid integer division, it casts one of the numbers to a float before calculating the quotient.

Uploaded by

Shanmuk_Ch
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
194 views

C Program To Do Arithmetic

The document describes a C program that takes in two numbers as input and calculates the sum, difference, product, quotient, and modulus of the two numbers. It includes sample input and output. The program first prompts the user to input two integers, then performs the five arithmetic operations on those numbers and prints out the results. To avoid integer division, it casts one of the numbers to a float before calculating the quotient.

Uploaded by

Shanmuk_Ch
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a C program to input two numbers and perform all arithmetic operations.

How
to perform all arithmetic operation between two numbers in C programming. C program
to find sum, difference, product, quotient and modulus of two given numbers.

Example
Input
First number: 10
Second number: 5
Output

Sum = 15
Difference = 5
Product = 50
Quotient = 2
Modulus = 0
Required knowledge
Arithmetic operators, Data types, Basic Input/Output

In previous post I explained to find the sum of two numbers.

Read more - Program to find sum of two numbers

In this exercise, we will pedal bit more and compute results of all arithmetic
operations at once.
Program to perform all arithmetic operations

/**
* C program to perform all arithmetic operations
*/

#include <stdio.h>

int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;

/*
* Input two numbers from user
*/
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);

/*
* Perform all arithmetic operations
*/
sum = num1 + num2;
sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;

/*
* Print result of all arithmetic operations
*/
printf("SUM = %d\n", sum);
printf("DIFFERENCE = %d\n", sub);
printf("PRODUCT = %d\n", mult);
printf("QUOTIENT = %f\n", div);
printf("MODULUS = %d", mod);

return 0;
}
In statement div = (float) num1 / num2;, I have typecasted num1 to float before the
divide operation, to avoid integer division.

Read more - Type conversion in C programming.

Note: \n is an escape sequence character used to print new lines (move to the next
line).

Output
Enter any two numbers : 20 10
SUM = 30
DIFFERENCE = 10
PRODUCT = 200
QUOTIENT = 2.000000
MODULUS = 0

You might also like