C Program to Find Product of Two Numbers using Recursion

This is a C program to find product of 2 numbers using recursion.

Problem Description

This C program finds the product of 2 numbers using recursion.

Problem Solution

This C program using recursion, finds the product of 2 numbers without using the multiplication operator.

Program/Source Code

Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*  
 * C Program to find Product of 2 Numbers using Recursion
 */
#include <stdio.h>
 
int product(int, int);
 
int main()
{
    int a, b, result;
 
    printf("Enter two numbers to find their product: ");
    scanf("%d%d", &a, &b);
    result = product(a, b);
    printf("Product of %d and %d is %d\n", a, b, result);
    return 0;
}
 
int product(int a, int b)
{
    if (a < b)
    {
        return product(b, a);
    }
    else if (b != 0)
    {
        return (a + product(a, b - 1));
    }
    else
    {
        return 0;
    }
}
Program Explanation

In this C program, reading two numbers using ‘a’ and ‘b’ variables respectively. The product() function is used to find the product of two numbers. Nested if else condition statement is used to check the value of ‘a’ variable is less than the value of ‘b’ variable.

advertisement
advertisement

If the condition is true then execute the statement. Compute the summation of the value of ‘a’ variable with the value. Otherwise, if the condition is false then execute else if condition statement. Check the condition that the value of ‘b’ variable is not equal to 0.

If the condition is true then execute the statement. Otherwise, if the condition is false then execute the else statement and return the null. Print the product of two numbers.

Runtime Test Cases
 
$ cc pgm20.c
$ a.out
Enter two numbers to find their product: 176 340
Product of 176 and 340 is 59840

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.