﻿ C# Sharp Exercises: Find the LCM and GCD of two numbers - w3resource

# C# Sharp Exercises: Find the LCM and GCD of two numbers

## C# Sharp Recursion: Exercise-12 with Solution

Write a program in C# Sharp to find the LCM and GCD of two numbers using recursion.

Pictorial Presentation:

Sample Solution:

C# Sharp Code:

``````using System;
using System.Text;

class RecExercise12
{
public static void Main()
{
long num1, num2, hcf, lcm;
Console.WriteLine("\n\n Recursion : Find the LCM and GCD of two numbers :");
Console.WriteLine("------------------------------------------------------");

Console.Write(" Input the first number : ");
Console.Write(" Input the second number : ");

hcf = gcd(num1, num2);
lcm = (num1 * num2) / hcf;

Console.WriteLine("\n The GCD of {0} and {1} = {2} ", num1, num2, hcf);
Console.WriteLine(" The LCM of {0} and {1} = {2}\n", num1, num2, lcm);

}

static long gcd(long n1, long n2)
{
if (n2 == 0)
{
return n1;
}
else
{
return gcd(n2, n1 % n2);
}
}
}
```
```

Sample Output:

``` Recursion : Find the LCM and GCD of two numbers :
------------------------------------------------------
Input the first number : 2
Input the second number : 5

The GCD of 2 and 5 = 1
The LCM of 2 and 5 = 10
```

Flowchart :

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

﻿

New Content: Composer: Dependency manager for PHP, R Programming