GCD of Two Numbers asked in PrepSter

Problem : To find GCD of two numbers.
Disclaimer-: The questions provided on this page are only model practice questions there is no surety that these questions have been previously asked in any company placement papers, these questions here only have the sole purpose to make you practice coding questions

15 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);

    }

    }


  • Karthikeyan

    a,b=[int(i) for i in input().split()] #input
    gcd = lambda a,b: a if b ==0 else not a % b and b or gcd(b , a % b) #GCD caluclation
    gcd(a,b) #Result print


  • 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;
      }