C program to add subtract divide multiply two numbers

Write a C program to input two numbers and perform addition, subtraction, multiplication, division and modulus operations of two numbers. C program to perform all arithmetic operations of given two numbers.

Example:
Input first number: 10
Input second number: 5
Output:
Sum = 15
Difference = 5
Product = 50
Quotient = 2
Modulus = 0

Required knowledge:

Fundamentals of C, Data types, Taking user input in C

Program:

/**
 * C program to perform addition, subtraction, multiplication, division two numbers
 */

#include <stdio.h>

int main()
{
    int num1, num2;
    int sum, sub, mult, mod;
    float div;

    /*
     * Read two numbers from user
     */
    printf("Enter any two numbers : \n");
    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("SUM = %d\n", sum);
    printf("DIFFERENCE = %d\n", sub);
    printf("PRODUCT = %d\n", mult);
    printf("QUOTIENT = %f\n", div);
    printf("MODULUS = %d", mod);

    return 0; 
}


Output
X
_
Enter any two numbers :
20
10
SUM = 30
DIFFERENCE = 10
PRODUCT = 200
QUOTIENT = 2.000000
MODULUS = 0

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

1 comment: