Skip to content

Commit

Permalink
Find GCD of Two Numbers using Euclidean's Algorithm Yet-Another-Serie…
Browse files Browse the repository at this point in the history
  • Loading branch information
palakjadwani committed Oct 19, 2019
1 parent 8b91c65 commit 0cc91a7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Algorithms/GCD_Euclidean/C_gcd_euclidean.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// C program to demonstrate Basic Euclidean Algorithm
#include <stdio.h>

// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}

// Driver program to test above function
int main()
{
int a,b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("GCD(%d, %d) = %d", a, b, gcd(a, b));
return 0;
}
29 changes: 29 additions & 0 deletions Algorithms/GCD_Euclidean/Java_gcd_euclidean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Java program to demonstrate working of extended
// Euclidean Algorithm

import java.util.*;
import java.lang.*;

class Java_gcd_euclidean
{
// extended Euclidean Algorithm
public static int gcd(int a, int b)
{
if (a == 0)
return b;

return gcd(b%a, a);
}

// Driver Program
public static void main(String[] args)
{
System.out.println("Enter two numbers: ");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
Scanner sc2 = new Scanner(System.in);
int b = sc.nextInt();
int g = gcd(a, b);
System.out.println("GCD(" + a + " , " + b+ ") = " + g);
}
}
25 changes: 25 additions & 0 deletions Algorithms/GCD_Euclidean/cpp_gcd_euclidean.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//C++ program to demonstrate
// Basic Euclidean Algorithm
#include <bits/stdc++.h>
using namespace std;

// Function to return
// gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}

// Driver Code
int main()
{
int a,b;
cout<<"Enter two numbers:";
cin>>a>>b;
cout << "GCD(" << a << ", "
<< b << ") = " << gcd(a, b)
<< endl;
return 0;
}
13 changes: 13 additions & 0 deletions Algorithms/GCD_Euclidean/python3_gcd_euclidean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Python program to demonstrate Basic Euclidean Algorithm


# Function to return gcd of a and b
def gcd(a, b):
if a == 0 :
return b

return gcd(b%a, a)

a = int(input('Enter first number: '))
b = int(input('Enter second number: '))
print("gcd(", a , "," , b, ") = ", gcd(a, b))

0 comments on commit 0cc91a7

Please sign in to comment.