C Program to Multiply Two Numbers Using Function

Howdy readers, today you will learn how to write a program to multiply two numbers using a function in C Programming language.

C Program to Multiply Two Numbers Using Function

In the previous post, you have seen how to multiply two numbers using the standard method. In this post, we are going to write a program that will calculate the product of two numbers using a user-defined function.

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

C Program to Multiply Two Numbers Using Function

C Program

// C Program to Multiply Two Numbers Using Function
#include <stdio.h>

int multiplyTwo(int x, int y){
    return (x * y);
}

int main(){
    int a, b, prod;
    
    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);
    
    // Calling out user-defined function
    prod = multiplyTwo(a, b);
    
    // Displaying result
    printf("Product of %d and %d is: %d", a, b, prod);
    
    return 0;
}

Output

Enter the first number: 4
Enter the second number: 7
Product of 4 and 7 is: 28
C Program to Multiply Two Numbers Using Function

Explanation

int multiplyTwo(int x, int y){
    return (x * y);
}

In this program, we have defined a custom function named multiplyTwo() which takes two numbers as an argument and returns the product of those numbers using the (*) operator.

    // Calling out user-defined function
    prod = multiplyTwo(a, b);

Then, we call the custom function in the main function. The product of two numbers gets stored in the prod named variable.

    // Displaying result
    printf("Product of %d and %d is: %d", a, b, prod);

Then, we simply print the product of two numbers using printf() function.

Conclusion

I hope after reading this post, you understand how to write a program to multiply two numbers using a function in the C Programming language.

If you have any queries regarding the program, then let us know in the comment section. We will be glad to solve your queries.

Leave a Comment