dotnet-Snippets.com
Snippets: 75 | Registered User: 84 | Visitors online: 0
Main Menu

Home
Random Snippet
FAQs
Contact Us
Imprint
RSS Feeds

Rss All languages
Rss C#
Rss VB.NET
Rss C++
Rss J#
Rss ASP.NET
Jobs

dotnet Jobs
Google Ads

Sri Lanka .NET 
                Forum Member
Recursive Euclidean Algorithm (for GCD)

Author: Guest
Programming Language: C# Rating:
not yet rated

Views: 95

Description:

A recursive algorithm used to find the GCD of two numbers.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/// <summary>
/// Finds the GCD of two numbers. (Recursively)
/// </summary>
public int gcd(int a, int b)
{
     if(a == 0)
          return a;
     if(b == 0)
          return b;

     if(a>b)
          return gcd(a % b, b);
     else
          return gcd(a, b % a);
}

This Snippets could be interesting for you:
No results available

Poor Excellent
1 2 3 4 5 6 7 8 9 10
Sign in to vote for this snippet.

Comments:
(Please log in to write an comment.)