# GCD of Two Numbers asked in PrepSter

##### Problem : To find GCD of two numbers.
`#include <iostream>using namespace std;int gcd(int a, int b){	if (b == 0)		return a;	return gcd(b, a % b);}int main(){    int a,b;    cin>>a;    cin>>b;    cout<<gcd(a,b);	return 0;}`
`import java.util.*;class Main {    public static int gcd(int a,int b)    {        if(b==0)            return a;        return gcd(b,a%b);    }    public static void main(String[] args)    {        Scanner sc=new Scanner(System.in);        int a=sc.nextInt();        int b=sc.nextInt();        System.out.println(gcd(a,b));    }}`

### 14 comments on “GCD of Two Numbers asked in PrepSter”

• vijay

java:
import java.util.Scanner;

public class FindGcd {

public static void main(String[] args)
{

@SuppressWarnings(“resource”)
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
int gcd=1;
//running loop form 1 to the smallest of both numbers
for(int i = 1; i <= x && i <= y; i++)
{
//returns true if both conditions are satisfied
if(x%i==0 && y%i==0)
{
//storing the variable i in the variable
gcd = i;

}

}

System.out.println("GCD of two numbers is:"+gcd);

}

}

• Nesar

PYTHON
from math import *
x = int(input())
y = int(input())
print(gcd(x, y))

• WHITEHILL_PPL

GCD of two numbers in python

gcd = 0
#print(“Enter two numbers : “)
a = int(input())
b = int(input())
i = 1
while(i <= a and i <= b):
if((a % i == 0) and (b % i == 0)):
gcd = i
i = i + 1

print("GCD : ",end="")
print(gcd)

• WHITEHILL_PPL

Gcd of two numbers in python
#gcd

gcd = 0
#print(“Enter two numbers : “)
a = int(input())
b = int(input())
i = 1
while(i <= a and i <= b):
if((a % i == 0) and (b % i == 0)):
gcd = i
i = i + 1

print("GCD : ",end="")
print(gcd)

• WHITEHILL_PPL

#gcd

gcd = 0
#print(“Enter two numbers : “)
a = int(input())
b = int(input())
i = 1
while(i <= a and i <= b):
if((a % i == 0) and (b % i == 0)):
gcd = i
i = i + 1

print("GCD : ",end="")
print(gcd)

• Saravanakumar

#include
using namespace std;
int gcd(int a,int b)
{
if(b==0)
return a;
else
gcd(b,a%b);
}
int main() {
int a,b;
cin>>a>>b;
cout<<gcd(a,b);
return 0;
}

• Amit

a=int(input())
b=int(input())
Min=min(a,b)
mat=[]
for i in range(1,Min+1):
if a%i==0 and b%i==0:
mat.append(i)
print(max(mat))

• SUNNY

import java.util.Scanner;

public class Main
{
public static void main(String[] args) {
System.out.println(“Hello World”);
Scanner sc=new Scanner(System.in);
int gcd=1;
System.out.println(” enter 1st number”);
int n1=sc.nextInt();
System.out.println(” enter 2nd number”);
int n2=sc.nextInt();
for(int i=1;i<=n1;i++){
if(n1%i==0 && n2%i==0){
gcd=i;
}
}
System.out.println(" GCD of two number is "+gcd);
}
}

• Akshay chauhan

#include

int gcd_iter(int u, int v)
{
int t;
while (v)
{
t = u;
u = v;
v = t % v;
}
return u < 0 ? -u : u; } int main() { int n=3,m=6; int result=gcd_iter(n,m); printf("%d",result); return 0; }

• prasanth

#include
int main()
{
int a=24,b=48,n=100,gcd;
for(i=0;i<=n;i++)
{
if(a%i==0 && b%i==0)
{
gcd==i;
}
}
printf("the gcd of two numbers is %d",gcd);
return 0;
}