Q:

Arithmetic operations in C

0
Arithmetic operations in C

C program to perform basic arithmetic operations of addition, subtraction, multiplication, and division of two numbers/integers that user inputs.

Division in C

In C language, when we divide two integers, we get an integer result, e.g., 5/2 evaluates to 2.

As a general rule integer/integer = integer, float/integer = float and integer/float = float. So we convert denominator to float in our program, you may also write float in the numerator. This explicit conversion is known as typecasting.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
int main()
{
   int first, second, add, subtract, multiply;
   float divide;
 
   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);
 
   add = first + second;
   subtract = first - second;
   multiply = first * second;
   divide = first / (float)second;   //typecasting, you can also write: divide = (float)first/second

   printf("Sum = %d\n", add);
   printf("Difference = %d\n", subtract);
   printf("Multiplication = %d\n", multiply);
   printf("Division = %.2f\n", divide); // "%.2lf" to print two decimal digits, by default (%lf) we get six
 
   return 0;
}

output:

Enter two integers

2

3

Sum = 5

Difference = -1

Multiplication = 6

Division = 0.67

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now