w3resource logo


C Programming Exercises

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

Secondary Nav

C Input Output statement: Exercise-9 with Solution

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

Test Data : 15, 5

Expected Output :
The sum of the given numbers : 20
The difference of the given numbers : 10
The product of the given numbers : 75
The quotient of the given numbers : 3.000000

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", mod);  
  
    return 0;   
}  

 

Solution

#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", mod);  
  
    return 0;   
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables