Arithmetic Operation Of Two Numbers


Explanation

Functions help us divide a program into smaller, reusable parts. In this program, four user-defined functions are created to perform the basic arithmetic operations: add(), sub(), mul(), and div(). Each function accepts two numbers as parameters, performs the required operation, and displays the result. The division function also checks whether the second number is zero to avoid division by zero.

How It Works?

  • Step 1: Include the required header files stdio.h and conio.h.
  • Step 2: Create four user-defined functions: add(), sub(), mul(), and div().
  • Step 3: Each function accepts two integer parameters.
  • Step 4: Perform addition, subtraction, multiplication, and division inside their respective functions.
  • Step 5: In the div() function, check whether the second number is zero before dividing.
  • Step 6: Call all the functions from the main() function using different values.
  • Step 7: Display the result of each arithmetic operation.

Example Program


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

void add(int a, int b)
{
    int k = a + b;
    printf("Addition of Numbers is: %d", k);
}

void sub(int a, int b)
{
    int k = a - b;
    printf("\nSubtraction of Numbers is: %d", k);
}

void mul(int a, int b)
{
    int k = a * b;
    printf("\nMultiplication of Numbers is: %d", k);
}

void div(int a, int b)
{

    if(b != 0)
    {

        int k = a / b;
        printf("\nDivision of Numbers is: %d", k);

    }

    else
    {

        printf("\nError: Division by zero is not allowed.");

    }

}

int main()
{

    clrscr();

    add(10, 20);

    sub(20, 10);

    mul(5, 5);

    div(10, 2);

    div(10, 0);

    getch();

}

Output

Addition of Numbers is: 30
Subtraction of Numbers is: 10
Multiplication of Numbers is: 25
Division of Numbers is: 5
Error: Division by zero is not allowed.

Practice Exercises

Task 1: Write a C program using four user-defined functions add(), sub(), mul(), and div() to perform arithmetic operations on the numbers 40 and 8. Display the result of each operation.

Goal: Learn how to create multiple user-defined functions and call them from the main() function to perform different arithmetic operations.

💡 Hint: Create four separate functions for addition, subtraction, multiplication, and division. Pass two numbers as arguments to each function and print the result. In the division function, check that the second number is not zero before performing division.
💡 Show Solution

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

void add(int a, int b)
{
    int k = a + b;
    printf("Addition = %d", k);
}

void sub(int a, int b)
{
    int k = a - b;
    printf("\nSubtraction = %d", k);
}

void mul(int a, int b)
{
    int k = a * b;
    printf("\nMultiplication = %d", k);
}

void div(int a, int b)
{

    if(b != 0)
    {
        int k = a / b;
        printf("\nDivision = %d", k);
    }

    else
    {
        printf("\nDivision by zero is not allowed.");
    }

}

int main()
{

    clrscr();

    add(40, 8);

    sub(40, 8);

    mul(40, 8);

    div(40, 8);

    getch();

}

Output
Addition = 48
Subtraction = 32
Multiplication = 320
Division = 5
Explanation: The program defines four separate user-defined functions to perform addition, subtraction, multiplication, and division. Each function receives two numbers as parameters, performs the required arithmetic operation, and displays the result. The main() function calls all four functions using the values 40 and 8, demonstrating how different functions can be used together in a single program.