Signup/Sign In

C Program to find Product of 2 Numbers using Recursion

learn C language Tutorial

Logic To Find Product Of 2 Numbers Using Recursion:

  • Get the inputs from the user and store it in the variables x and y,
  • The function product is used to calculate the product of given numbers,
  • A nested if-else statement is used to check x is less than y,
  • if the condition is satisfied, add the value of x with the value, else execute the else-if condition,
  • check the value of y is not equal to 0,
  • If the condition statement is true, execute the statement, or if the condition is not satisfied execute else block,
  • Print the product of two numbers.

C Program To Find Product Of 2 Numbers Using Recursion:

#include <stdio.h>
 
int product(int, int);
 
int main()
{
    int x, y, pro;
 
    printf("Enter The Numbers To Find The Product: ");
    scanf("%d%d", &x, &y);
    pro = product(x, y);
    printf("Product Of The Given Numbers is %d\n",  pro);
    return 0;
}
 
int product(int x, int y)
{
    if (x < y)
    {
        return product(y, x);
    }
    else if (y != 0)
    {
        return (x + product(x, y - 1));
    }
    else
    {
        return 0;
    }
}

Output:

Product