-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Find GCD of Two Numbers using Euclidean's Algorithm Yet-Another-Serie…
- Loading branch information
1 parent
8b91c65
commit 0cc91a7
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |