Arithmetic Operations Using scanf() in C

Definition:

The scanf() function in C is used to accept input from the user through the keyboard. It allows users to enter values during program execution instead of using fixed values.

Explanation:

This program accepts two numbers from the user using the scanf() function. It then performs addition, subtraction, multiplication, and division on the entered numbers and displays the results using the printf() function.


Program: Addition of Two Numbers Using scanf()

Explanation: This program accepts two numbers from the user using the scanf() function and displays their sum.


#include <stdio.h>
#include <conio.h>

main()
{
    int a,b,sum;

    clrscr();

    printf("Enter First Number : ");
    scanf("%d",&a);

    printf("Enter Second Number : ");
    scanf("%d",&b);

    sum = a + b;

    printf("\nAddition = %d",sum);

    getch();
}

Output:

Enter First Number : 12
Enter Second Number : 8

Addition = 20

Program: Multiplication of Two Numbers Using scanf()

Explanation: This program accepts two numbers from the user using the scanf() function and displays their product.


#include <stdio.h>
#include <conio.h>

main()
{
    int a,b,multi;

    clrscr();

    printf("Enter First Number : ");
    scanf("%d",&a);

    printf("Enter Second Number : ");
    scanf("%d",&b);

    multi = a * b;

    printf("\nMultiplication = %d",multi);

    getch();
}

Output:

Enter First Number : 15
Enter Second Number : 4

Multiplication = 60

Program: Arithmetic Operations Using scanf()

Explanation: This program accepts two numbers from the user and performs addition, subtraction, multiplication, and division.


#include <stdio.h>
#include <conio.h>

main()
{
    int a,b,add,sub,multi,div;

    clrscr();

    printf("Enter The Value Of A : ");
    scanf("%d",&a);

    printf("Enter The Value Of B : ");
    scanf("%d",&b);

    add = a + b;
    sub = a - b;
    multi = a * b;
    div = a / b;

    printf("\nAddition = %d",add);
    printf("\nSubtraction = %d",sub);
    printf("\nMultiplication = %d",multi);
    printf("\nDivision = %d",div);

    getch();
}

Output:

Enter The Value Of A : 20
Enter The Value Of B : 10

Addition = 30
Subtraction = 10
Multiplication = 200
Division = 2

Practice Exercises

Task 1: Write a C program to accept two numbers from the user and display their addition.

Goal: Learn how to use scanf() for user input and printf() for output.
💡 Hint: Use two integer variables and the + operator.
💡 Show Solution

#include <stdio.h>

main()
{
    int a,b;

    scanf("%d%d",&a,&b);

    printf("Addition = %d",a+b);

    getch();
}

Output
Input: 20 30 Addition = 50
Explanation: The program accepts two numbers from the user and displays their sum.
Task 2: Write a C program to accept two numbers and display their subtraction.

Goal: Learn subtraction using user input.
💡 Hint: Use the - operator.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    int a,b;

    scanf("%d%d",&a,&b);

    printf("Subtraction = %d",a-b);

    getch();
}

Output
Input: 25 10 Subtraction = 15
Explanation: The program subtracts the second number from the first number.
Task 3: Write a C program to accept two numbers from the user and display their multiplication.

Goal: Practice multiplication using the scanf() function.
💡 Hint: Use the * operator after accepting two numbers.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    int a,b;

    scanf("%d%d",&a,&b);

    printf("Multiplication = %d",a*b);

    getch();
}

Output
Input: 9 8 Multiplication = 72
Explanation: The program accepts two numbers from the user and displays their product.