C Program to Divide Two Numbers Using Functions

Howdy readers, today you will learn how to write a C program to divide two numbers using functions.

This tutorial takes two numbers as input from the user using the scanf() function and stores the two numbers in the two variables num and den.

Then, it calls out the function Divide which we have declared to divide two numbers and return the result.

The answer returned from the custom function Divide gets stored in the result variable and the result is displayed as output.

So, without any delay, let’s begin this tutorial.

C Program to Divide Two Numbers Using Functions

C Program

// C Program to Divide Two Numbers Using Functions
#include <stdio.h>

// Declaring and Defining Function
int Divide(int num1, int num2){
    return (num1 / num2);
}

int main(){
    int num, den, result;
    
    // Taking Input
    printf("Enter the first number: ");
    scanf("%d", &num);
    printf("Enter the second number: ");
    scanf("%d", &den);
    
    // Calling Function
    result = Divide(num, den);
    
    // Display Result
    printf("The division of %d and %d is: %d", num, den, result);
    
    return 0;
}

Output

Enter the first number: 50
Enter the second number: 7
The division of 50 and 7 is: 7
C Program to Divide Two Numbers Using Functions

Explanation

int Divide(int num1, int num2){
    return (num1 / num2);
}

We have declared a custom function Divide with two parameters. Within the function, the division is performed using the division operator (/).

     int num, den, result;

In the main function, we have declared three integer data type variables num, den and result.

    printf("Enter the first number: ");
    scanf("%d", &num);
    printf("Enter the second number: ");
    scanf("%d", &den);

The two numbers are taken as input from the user using the scanf() function. The two numbers get stored in the num and den variables respectively.

    result = Divide(num, den);

After that, we call the custom function Divide in the main function. The two variables num and den are arguments. The Divide function divides the values of two arguments and returns the answer. 

Note: When a parameter is passed to the function, it is called an argument. In the above example, num1 and num2 are parameters and num and den are arguments.

The returned value gets stored in the result variable.

    printf("The division of %d and %d is: %d", num, den, result);

The result of the division of two numbers is printed on the screen using the printf() function.

Conclusion

Today you learned about the C program to divide two numbers using functions.

Don’t forget to comment down your queries in the comment section.

Thanks for reading.

Happy Coding!!

Also Read:

Leave a Comment