Find GCD of Two Numbers
Ques: Problem: To find GCD of two number.
NOTE:- Please comment down the code in other languages as well below –
C
C++
Java
Python
C
Run
#includeint findGCD(int a, int b) { if (b == 0) return a; return findGCD(b, a % b); } int main() { int num1, num2; printf("Enter the first number: "); scanf("%d", &num1); printf("Enter the second number: "); scanf("%d", &num2); int gcd = findGCD(num1, num2); printf("GCD: %d\n", gcd); return 0; }
C++
Run
#includeusing namespace std; int findGCD(int a, int b) { if (b == 0) return a; return findGCD(b, a % b); } int main() { int num1, num2; cout << "Enter the first number: "; cin >> num1; cout << "Enter the second number: "; cin >> num2; int gcd = findGCD(num1, num2); cout << "GCD: " << gcd << endl; return 0; }
Java
Run
import java.util.Scanner; public class Main { public static int findGCD(int a, int b) { if (b == 0) return a; return findGCD(b, a % b); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); scanner.close(); int gcd = findGCD(num1, num2); System.out.println("GCD: " + gcd); } }
Python
Run
def find_gcd(a, b): while b: a, b = b, a % b return a num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) gcd = find_gcd(num1, num2) print("GCD:", gcd)
code in python:-
def GCDnum(n,m):
while(n):
t = m
m = n
n = t % n
return m if m>0 else -m
x = 3
y = 6
print(GCDnum(x,y))
JAVA PROGRAM….
import java.util.Scanner;
class Test{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
System.out.println(“GCD is:” findGCD(x,y));
}
static int findGCD(int x, int y)
{
int r=0, a, b;
a=(x>y)?x:y; //a is greater
b=(x<y)? x: y; //b is smaller
r=b;
while(a%b !=0)
{
r=a%b;
a=b;
b=r;
}
return r;
}
}
#include
int HCF_cal(int a, int b)
{
int HCF;
for(HCF = a = 1; HCF–)
{
if(a%HCF==0 && b%HCF==0)
{
break;
}
}
return HCF;
}
int main()
{
int n1, n2, Result;
scanf(“%d %d”, &n1, &n2);
Result = HCF_cal(n1, n2);
printf(“HCF(%d, %d) = %d”, n1, n2, Result);
}
#include
int HCF_cal(int a, int b)
{
int HCF;
for(HCF = a = 1; HCF–)
{
if(a%HCF==0 && b%HCF==0)
{
break;
}
}
return HCF;
}
int main()
{
int n1, n2, Result;
scanf(“%d %d”, &n1, &n2);
Result = HCF_cal(n1, n2);
printf(“HCF(%d, %d) = %d”, n1, n2, Result);
}
in python ——-
import math
num1 = int(input(“enter number 1”))
num2 = int(input(“enter number 2”))
print(math.gcd(num1,num2))
plz show in python language
python
import math
num1=int(input(“enter the number”))
num2=int(input(“enter the number2”))
print()
print(math.gcd(num1,num2))
#include
int main()
{
int m,n,i,gcd=1,min;
scanf(“%d%d”,&n,&m);
if(n>=m)
{
min=m;
}
else
{
min=n;
}
for(i=1;i<=min;i++)
{
if((n%i==0)&&(m%i==0))
{
gcd=i;
}
}
printf("%d",gcd);
return 0;
}
#include
int gcd(int a, int b);
int main()
{
int n,m,hcf;
printf(“Enter the two value:”);
scanf(“%d%d”,&n,&m);
hcf=gcd(n,m);
printf(“hcf=%d”,hcf);
return 0;
}
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
C
This is very helpful to all students
Hshs
#include
using namespace std;
gcd(int a,int b)
{
if(a==0)
return b;
return gcd(b%a,a);
}
int main()
{
int a,b;
cin>>a>>b;
cout<<gcd(a,b);
}
import math
n,k=input().split()
n=int(n)
k=int(k)
g=math.gcd(n,k)
print(g)