Program for gcd of two numbers
Table of Contents
- Program for gcd of two numbers in java
- Program for gcd of two numbers in c++
- Program for gcd of two numbers in c
- Program for gcd of two numbers in golang
- Program for gcd of two numbers in c#
- Program for gcd of two numbers in vb.net
- Program for gcd of two numbers in php
- Program for gcd of two numbers in node js
- Program for gcd of two numbers in typescript
- Program for gcd of two numbers in python
- Program for gcd of two numbers in ruby
- Program for gcd of two numbers in scala
- Program for gcd of two numbers in swift
- Program for gcd of two numbers in kotlin
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.
-
1) GCD of two numbers in java
2) GCD of two numbers in c++
3) GCD of two numbers in c#
4) GCD of two numbers in php
5) GCD of two numbers in node js
6) GCD of two numbers in python
7) GCD of two numbers in ruby
8) GCD of two numbers in scala
9) GCD of two numbers in swift
10) GCD of two numbers in golang
11) GCD of two numbers in typescript
12) GCD of two numbers in kotlin
13) GCD of two numbers in vb.net
14) Gcd of two numbers using recursion in c
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