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 Product of 2 Numbers using Recursion.

Problem Solution

Here the multiples of 3 and 5 are found and the sum of all the multiples are calculated and are displayed.

Program/Source Code

Here is source code of the C# Program to find Product of 2 Numbers using Recursion. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to find Product of 2 Numbers using Recursion
 */
using System;
class program
{
    public static void Main()
    {
        int a, b, result;
        Console.WriteLine("Enter two numbers to find their product: ");
        a = int.Parse(Console.ReadLine());
        b = int.Parse(Console.ReadLine());
        prog pg = new prog();
        result = pg.product(a, b);
        Console.WriteLine("Product of {0} and {1} is {2}",a, b, result);
        Console.ReadLine();
    }
}
class prog
{
    public 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, we are reading two numbers using ‘a’ and ‘b’ variables respectively. Using ‘result’ variable we are calling product() function by passing the value of ‘a’ and ‘b’ variables as an argument.

advertisement
advertisement

Then product() function is used to find the multiples of 3 & 5 and the sum of all the multiples are calculated and are printed.

Runtime Test Cases
 
Enter two numbers to find their product:
5
6
Product of 5 and 6 is 30

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

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.