Addition Multiplication Subtraction Division in C

Addition Multiplication Subtraction Division in C Programming | Here we write a program to perform basic arithmetic operations like addition subtraction multiplication and division in C. Write a C program to perform addition subtraction multiplication and division of two numbers.

C Program to Compute Addition Subtraction and Multiplication

#include<stdio.h>
int main()
{
     // declare variables
     int num1, num2, sum, subtract, multiple;

     // take inputs
     printf("Enter two integers: ");
     scanf("%d %d", &num1, &num2);

     // calculate addition
     sum = num1 + num2;

     // calculate subtraction
     subtract = num1 - num2;

     // calculate multiplocation
     multiple = num1 * num2;

     // display results
     printf("%d + %d = %d\n", num1, num2, sum);
     printf("%d - %d = %d\n", num1, num2, subtract);
     printf("%d * %d = %d\n", num1, num2, multiple);

     return 0;
}

Output:-

Enter two integers: 50 30
50 + 30 = 80
50 – 30 = 20
50 * 30 = 1500

Two integers entered by user is stored in variables num1 and num2 using scanf() function. Sum of two numbers will stored in variable sum, similarly happen with subtract and multiple.

C Program to Compute Quotient and Remainder

#include<stdio.h>
int main()
{
     // declare variables
     int dividend, divisor, quotient, remainder;

     // take inputs
     printf("Enter dividend: ");
     scanf("%d", &dividend);
     printf("Enter divisor: ");
     scanf("%d", &divisor);

     // Computes quotient
     quotient = dividend / divisor;
     // Computes remainder
     remainder = dividend % divisor;

     // display results
     printf("Quotient = %d\n", quotient);
     printf("Remainder = %d\n", remainder);
     return 0;
}

Output:-

Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1

In this program, the user is asked to enter two integers (dividend and divisor) which will be stored in variable dividend and divisor respectively. Then the quotient is evaluated using the division (/) operator and stored in the variable quotient.

Similarly, the remainder is evaluated using the modulus % operator and stored in a remainder variable. Finally, the quotient and remainder are displayed using printf() function.

In the C program, the % operator can’t apply on the floating-point number. But if we want to find the remainder on the floating-point number then we should use fmod() function defined under “math.h“, and fmod() function takes two-parameter and returns floating-point value.

#include<stdio.h>
#include<math.h>
int main()
{
   float a, b;

   printf("Enter two number: ");
   scanf("%f %f", &a, &b);

   printf("Product = %.2f\n", a*b);
   printf("Remainder = %.2f", fmod(a,b));

   return 0;
}

Output:-

Enter two number: 34 5
Product = 170.00
Remainder = 4.00

Note:-

  1. If you are not using windows operating system and getting the error: undefined reference to ‘fmod’, then read this undefined reference to sqrt() in C programming
  2. The fmod() function returns floating value so in the above program, if you use %d in place of %f then you will get output, Remainder=0
    printf("Remainder = %d", fmod(a,b));
    Output:-
    Remainder = 0

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Also see:- Sum & Average of 3 Numbers in C, Simple and Compound Interest, Distance between two Points

Leave a Comment

Your email address will not be published. Required fields are marked *