dotnet-Snippets.com
Snippets: 73 | Registered User: 84 | Visitors online: 20
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
Euclidean Algorithm for GCD

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

Views: 3675

Description:

Implementation of the Euclidean Algorithm for finding the Greatest Common Denominator between 2 integers.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int GCD(int a, int b)
{
     while (a != 0 && b != 0)
     {
         if (a > b)
            a %= b;
         else
            b %= a;
     }

     if (a == 0)
         return b;
     else
         return a;
}

This Snippets could be interesting for you:

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.)