Program for gcd of two numbers

Program for gcd of two numbers in java

/*
  Java program for
  Calculate GCD of two numbers using recursion
*/
public class GcdValue
{
	// Recursively find GCD of two given number
	public int gcd(int first, int second)
	{
		if (first == 0)
		{
			return second;
		}
		// Recursively call gcd
		return gcd(second % first, first);
	}
	public static void main(String[] args)
	{
		GcdValue task = new GcdValue();
		// Test Cases
		// a = 15 b = 20 = (5)
		System.out.println(task.gcd(15, 20));
		// a = 24 b = 75 = (3)
		System.out.println(task.gcd(24, 75));
		// a = 45 b = 30 = (15)
		System.out.println(task.gcd(45, 30));
		// a = 21 b = 49 = (7)
		System.out.println(task.gcd(21, 49));
		// a = 16 b = 40 = (8)
		System.out.println(task.gcd(16, 40));
	}
}
Output
5
3
15
7
8

Program for gcd of two numbers in c++

// Include header file
#include <iostream>
//  Stdc++11 program for
//  Calculate GCD of two numbers using recursion
class GcdValue
{
    // Recursively find GCD of two given number
    public:
    int gcd(int first, int second)
    {
        if (first == 0)
        {
            return second;
        }
        // Recursively call gcd
        return gcd(second % first, first);
    }
};
int main(int argc, char **argv){
    GcdValue task;
    // Test Cases
    // a = 15 b = 20 = (5)
    std::cout << task.gcd(15, 20) << std::endl;
    // a = 24 b = 75 = (3)
    std::cout << task.gcd(24, 75) << std::endl;
    // a = 45 b = 30 = (15)
    std::cout << task.gcd(45, 30) << std::endl;
    // a = 21 b = 49 = (7)
    std::cout << task.gcd(21, 49) << std::endl;
    // a = 16 b = 40 = (8)
    std::cout << task.gcd(16, 40) << std::endl;
	return 0;
};
Output
5
3
15
7
8

Program for gcd of two numbers in c

// Include header file
#include <stdio.h>
// C program for
// Calculate GCD of two numbers using recursion

// Recursively find GCD of two given number
int gcd(int first, int second) {
	if (first == 0) {
		return second;
	}
	// Recursively call gcd
	return gcd(second % first, first);
}
int main() {
	// Test Cases
	// a = 15 b = 20 = (5)
	printf("%d\n", gcd(15, 20));
	// a = 24 b = 75 = (3)
	printf("%d\n", gcd(24, 75));
	// a = 45 b = 30 = (15)
	printf("%d\n", gcd(45, 30));
	// a = 21 b = 49 = (7)
	printf("%d\n", gcd(21, 49));
	// a = 16 b = 40 = (8)
	printf("%d\n", gcd(16, 40));
	return 0;
}
Output
5
3
15
7
8

Program for gcd of two numbers in golang

package main
import "fmt"
//  Golang program for
//  Calculate GCD of two numbers using recursion

// Recursively find GCD of two given number
func gcd(first, second int)int {
    if (first == 0) {
        return second;
    }
    // Recursively call gcd
    return gcd(second % first, first);
}

func main() {
    // Test Cases
    // a = 15 b = 20 = (5)
    fmt.Println(gcd(15, 20));
    // a = 24 b = 75 = (3)
    fmt.Println(gcd(24, 75));
    // a = 45 b = 30 = (15)
    fmt.Println(gcd(45, 30));
    // a = 21 b = 49 = (7)
    fmt.Println(gcd(21, 49));
    // a = 16 b = 40 = (8)
    fmt.Println(gcd(16, 40));
 }
Output
5
3
15
7
8

Program for gcd of two numbers in c#

// Include namespace system
using System; 
//  C# program for
//  Calculate GCD of two numbers using recursion
public class GcdValue
{
    // Recursively find GCD of two given number
    public int gcd(int first, int second)
    {
        if (first == 0)
        {
            return second;
        }
        // Recursively call gcd
        return this.gcd(second % first, first);
    }
    public static void Main(String[] args)
    {
        var task = new GcdValue();
        // Test Cases
        // a = 15 b = 20 = (5)
        Console.WriteLine(task.gcd(15, 20));
        // a = 24 b = 75 = (3)
        Console.WriteLine(task.gcd(24, 75));
        // a = 45 b = 30 = (15)
        Console.WriteLine(task.gcd(45, 30));
        // a = 21 b = 49 = (7)
        Console.WriteLine(task.gcd(21, 49));
        // a = 16 b = 40 = (8)
        Console.WriteLine(task.gcd(16, 40));
    }
}
Output
5
3
15
7
8

Program for gcd of two numbers in vb.net

' Include namespace system
Imports System 
'  Vb.net program for
'  Calculate GCD of two numbers using recursion
public Class GcdValue
    ' Recursively find GCD of two given number
    Public Function  gcd(ByVal first As Integer, 
                         ByVal second As Integer) As Integer
        if (first = 0) Then
            Return  second
        End If
        ' Recursively call gcd
        Return  Me.gcd(second Mod first, first)
    End Function
    Public Shared Sub Main(ByVal args As String())
        Dim task As GcdValue = New GcdValue()
        ' Test Cases
        ' a = 15 b = 20 = (5)
        Console.WriteLine(task.gcd(15, 20))
        ' a = 24 b = 75 = (3)
        Console.WriteLine(task.gcd(24, 75))
        ' a = 45 b = 30 = (15)
        Console.WriteLine(task.gcd(45, 30))
        ' a = 21 b = 49 = (7)
        Console.WriteLine(task.gcd(21, 49))
        ' a = 16 b = 40 = (8)
        Console.WriteLine(task.gcd(16, 40))
    End Sub
End Class
Output
5
3
15
7
8

Program for gcd of two numbers in php

<?php 
//  Php program for
//  Calculate GCD of two numbers using recursion
class GcdValue
{
    // Recursively find GCD of two given number
    function gcd($first, $second)
    {
        if ($first == 0)
        {
            return $second;
        }
        // Recursively call gcd
        return $this->gcd($second % $first, $first);
    }
}
$task = new GcdValue();
// Test Cases
// a = 15 b = 20 = (5)
printf("%d\n",$task->gcd(15, 20));
// a = 24 b = 75 = (3)
printf("%d\n",$task->gcd(24, 75));
// a = 45 b = 30 = (15)
printf("%d\n",$task->gcd(45, 30));
// a = 21 b = 49 = (7)
printf("%d\n",$task->gcd(21, 49));
// a = 16 b = 40 = (8)
printf("%d\n",$task->gcd(16, 40));
Output
5
3
15
7
8

Program for gcd of two numbers in node js

//  Node Js program for
//  Calculate GCD of two numbers using recursion
class GcdValue
{
    // Recursively find GCD of two given number
    gcd(first, second)
    {
        if (first == 0)
        {
            return second;
        }
        // Recursively call gcd
        return this.gcd(second % first, first);
    }
}
// Start program execution
var task = new GcdValue();
// Test Cases
// a = 15 b = 20 = (5)
console.log(task.gcd(15, 20));
// a = 24 b = 75 = (3)
console.log(task.gcd(24, 75));
// a = 45 b = 30 = (15)
console.log(task.gcd(45, 30));
// a = 21 b = 49 = (7)
console.log(task.gcd(21, 49));
// a = 16 b = 40 = (8)
console.log(task.gcd(16, 40));
Output
5
3
15
7
8

Program for gcd of two numbers in typescript

//  Typescript program for
//  Calculate GCD of two numbers using recursion
class GcdValue
{
    // Recursively find GCD of two given number
    public number gcd(first:number, second:number)
    {
        if (first == 0)
        {
            return second;
        }
        // Recursively call gcd
        return this.gcd(second % first, first);
    }
}
var task = new GcdValue();
// Test Cases
// a = 15 b = 20 = (5)
console.log(task.gcd(15, 20));
// a = 24 b = 75 = (3)
console.log(task.gcd(24, 75));
// a = 45 b = 30 = (15)
console.log(task.gcd(45, 30));
// a = 21 b = 49 = (7)
console.log(task.gcd(21, 49));
// a = 16 b = 40 = (8)
console.log(task.gcd(16, 40));
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */
Output
5
3
15
7
8

Program for gcd of two numbers in python

#  Python 3 program for
#  Calculate GCD of two numbers using recursion

# Recursively find GCD of two given number
def  gcd(first,  second) :
    if (first == 0) :
        return second
    # Recursively call gcd
    return gcd(second % first, first)


if __name__=="__main__":

    # Test Cases
    # a = 15 b = 20 = (5)
    print(gcd(15, 20))
    # a = 24 b = 75 = (3)
    print(gcd(24, 75))
    # a = 45 b = 30 = (15)
    print(gcd(45, 30))
    # a = 21 b = 49 = (7)
    print(gcd(21, 49))
    # a = 16 b = 40 = (8)
    print(gcd(16, 40))
    
Output
5
3
15
7
8

Program for gcd of two numbers in ruby

#  Ruby program for
#  Calculate GCD of two numbers using recursion

# Recursively find GCD of two given number
def gcd( first,  second)
    if (first == 0)
        return second
    end
    # Recursively call gcd
    return gcd(second % first, first)
end

# Test Cases
# a = 15 b = 20 = (5)
print(gcd(15, 20),"\n")
# a = 24 b = 75 = (3)
print(gcd(24, 75),"\n")
# a = 45 b = 30 = (15)
print(gcd(45, 30),"\n")
# a = 21 b = 49 = (7)
print(gcd(21, 49),"\n")
# a = 16 b = 40 = (8)
print(gcd(16, 40),"\n")
Output
5
3
15
7
8

Program for gcd of two numbers in scala

//  Scala program for
//  Calculate GCD of two numbers using recursion
class GcdValue ()
{
    // Recursively find GCD of two given number
    def gcd(first : Int, second : Int) : Int=
    {
        if (first == 0)
        {
            return second
        }
        // Recursively call gcd
        return gcd(second % first, first)
    }
}

object Main 
{
	def main(args : Array[String]) : Unit=
    {
        var task = new GcdValue()
        // Test Cases
        // a = 15 b = 20 = (5)
        println(task.gcd(15, 20))
        // a = 24 b = 75 = (3)
        println(task.gcd(24, 75))
        // a = 45 b = 30 = (15)
        println(task.gcd(45, 30))
        // a = 21 b = 49 = (7)
        println(task.gcd(21, 49))
        // a = 16 b = 40 = (8)
        println(task.gcd(16, 40))
    }
}
Output
5
3
15
7
8

Program for gcd of two numbers in swift

import Foundation
//  Swift program for
//  Calculate GCD of two numbers using recursion
class GcdValue
{
    // Recursively find GCD of two given number
    func gcd(_ first : Int, _ second : Int) -> Int
    {
        if (first == 0)
        {
            return second;
        }
        // Recursively call gcd
        return self.gcd(second % first,first);
    }
}

let task : GcdValue = GcdValue();
// Test Cases
// a = 15 b = 20 = (5)
print(task.gcd(15,20));
// a = 24 b = 75 = (3)
print(task.gcd(24,75));
// a = 45 b = 30 = (15)
print(task.gcd(45,30));
// a = 21 b = 49 = (7)
print(task.gcd(21,49));
// a = 16 b = 40 = (8)
print(task.gcd(16,40));
Output
5
3
15
7
8

Program for gcd of two numbers in kotlin

//  Kotlin program for
//  Calculate GCD of two numbers using recursion
class GcdValue {
    // Recursively find GCD of two given number
    fun gcd(first : Int, second : Int) : Int
    {
        if (first == 0)
        {
            return second;
        }
        // Recursively call gcd
        return this.gcd(second % first, first);
    }
}
fun main(args : Array<String>) : Unit
{
    val task : GcdValue = GcdValue();
    // Test Cases
    // a = 15 b = 20 = (5)
    println(task.gcd(15, 20));
    // a = 24 b = 75 = (3)
    println(task.gcd(24, 75));
    // a = 45 b = 30 = (15)
    println(task.gcd(45, 30));
    // a = 21 b = 49 = (7)
    println(task.gcd(21, 49));
    // a = 16 b = 40 = (8)
    println(task.gcd(16, 40));
}
Output
5
3
15
7
8

Some standard solution by language.




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2022, kalkicode.com, All rights reserved