Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Program to find GCD or HCF of two numbers c#

// C# program to find GCD of two numbers
using System;
class GFG
{
    static int [,]dp = new int[1001, 1001];
   
    // Recursive function to return gcd of a and b
    static int gcd(int a, int b)
    {
       
        // Everything divides 0
        if (a == 0)
          return b;
        if (b == 0)
          return a;
      
        // base case
        if (a == b)
            return a;
      
    // if a value is already
    // present in dp
    if(dp[a, b] != -1)
        return dp[a, b];
 
    // a is greater
    if (a > b)
        dp[a, b] = gcd(a-b, b);
     
    // b is greater
    else
        dp[a, b] = gcd(a, b-a);
     
    // return dp
    return dp[a, b];
    }
     
    // Driver method
    public static void Main()
    {
        for(int i = 0; i < 1001; i++) {
            for(int j = 0; j < 1001; j++) {
                dp[i, j] = -1;
            }
        }
        int a = 98, b = 56;
        Console.Write("GCD of " + a +" and " + b + " is " + gcd(a, b));
    }
}
 
// This code is contributed by Samim Hossain Mondal.
Comment

PREVIOUS NEXT
Code Example
Csharp :: split string by 5 characters c# 
Csharp :: params keycord as var name c# 
Csharp :: C# parallel for loop specify cores 
Csharp :: how to add a round image unity 
Csharp :: nullable IList 
Csharp :: .netstandard distinctby iqueryable 
Csharp :: unity Polymorphism 
Csharp :: how to count specific controls in a container c# 
Csharp :: death transition unity 2d 
Csharp :: ExpandoObject Make Objects Extensible 
Csharp :: ip validation .net core 
Csharp :: unity make particles stay still 
Csharp :: .net form binding why cant i skip index 
Csharp :: c# extension method in non static class 
Csharp :: add integer to string c# 
Csharp :: cache.TryGetValue in MemoryCache c# .net 
Csharp :: attributes C# reflection variable update site:stackoverflow.com 
Csharp :: Smooth Sentences c# 
Csharp :: translate nicely between two vector3 
Csharp :: c# make a negative number positive 
Csharp :: unity reload script assemblies 
Csharp :: Create an array with random values c# 
Csharp :: build a project from dotnet using cli 
Csharp :: why doesnt the if command work in C# 
Csharp :: wie macht man eine schleife in c# 
Csharp :: dinero en C# 
Html :: font-awesome envelope 
Html :: regex href html pattern 
Html :: print page button html 
Html :: email anchor tag 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =