w3resource logo
C Programming Exercises

C Exercises: Perform addition, subtraction, multiplication and  division of two numbers

C Input Output statement: Exercise-9 with Solution

Write a C program to perform addition, subtraction, multiplication and  division of two numbers.

Sample Solution:

C Code:

#include <stdio.h>
int main()  
{  
    int num1, num2;  
    int sum, sub, mult, mod;  
    float div;  
  
    /* 
     * Read two numbers from user separated by comma 
     */  
    printf("Input any two numbers separated by comma : ");  
    scanf("%d,%d", &num1, &num2);  
  
    /* 
     * Performs all arithmetic operations 
     */   
    sum = num1 + num2;  
    sub = num1 - num2;  
    mult = num1 * num2;  
    div = (float)num1 / num2;  
    mod = num1 % num2;  
  
    /* 
     * Prints the result of all arithmetic operations 
     */  
    printf("The sum of the given numbers : %d\n", sum);  
    printf("The difference of the given numbers : %d\n", sub);  
    printf("The product of the given numbers : %d\n", mult);  
    printf("The quotient of the given numbers : %f\n", div);  
    printf("MODULUS = %d\n", mod);  
  
    return 0;   
} 
 

Sample Output:

Input any two numbers separated by comma : 10,5                                                               
The sum of the given numbers : 15                                                                             
The difference of the given numbers : 5                                                                       
The product of the given numbers : 50                                                                         
The quotient of the given numbers : 2.000000
MODULUS = 0 

C Programming Code Editor:

#include <stdio.h>

int main()  
{  
    int num1, num2;  
    int sum, sub, mult, mod;  
    float div;  
  
    /* 
     * Read two numbers from user separated by comma 
     */  
    printf("Input any two numbers separated by comma : ");  
    scanf("%d,%d", &num1, &num2);  
  
    /* 
     * Performs all arithmetic operations 
     */   
    sum = num1 + num2;  
    sub = num1 - num2;  
    mult = num1 * num2;  
    div = (float)num1 / num2;  
    mod = num1 % num2;  
  
    /* 
     * Prints the result of all arithmetic operations 
     */  
    printf("The sum of the given numbers : %d\n", sum);  
    printf("The difference of the given numbers : %d\n", sub);  
    printf("The product of the given numbers : %d\n", mult);  
    printf("The quotient of the given numbers : %f\n", div);  
    printf("MODULUS = %d\n", mod);  
  
    return 0;   
}

Improve this sample solution and post your code through Disqus.