Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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.

Pictorial Presentation:

C Input Output: 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 

Flowchart:

C Programming Input Output Flowchart: Perform addition, subtraction, multiplication and  division of two numbers.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to calculate the sum of three numbers with getting input in one line separated by a comma.
Next: Write a C  program to find the third angle of a triangle if two angles are given.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP