Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

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

12 comments on “GCD of Two Numbers asked in PrepSter”


  • 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))


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