# GCD of Two Numbers asked in Wipro NLTH

## Problem: To find GCD of two number.

```// C program to calculate GCD of two numbers
#include
// The code used a recursive function to return gcd of p and q
int gcd(int p, int q)
{

// checking divisibility by 0
if (p == 0)
return q;
if (q == 0)
return p;

// base case
if (p == q)
return p;
// p is greater
if (p > q)
return gcd(p-q, q);

else
return gcd(p, q-p);
}

// Driver program to test above function
int main()
{     int p = 98, q = 56;
printf("GCD of %d and %d is %d ", p, q, gcd(p, q));
return 0;
}

```

Detailed program to find GCD OR HCF of two numbers in C

### C++ code:

`//C++ Program//GCD of Two Numbers#include<iostream>using namespace std;// Recursive function declarationint findGCD(int, int);// main programint main(){        int first, second;        cout<<"Enter First Number: ";        cin>>first;        cout<<"Enter second Number: ";        cin>>second;        cout<<"GCD of "<<first<<" and "<<second<<" is "<<findGCD(first,second);        return 0;}//body of the functionint findGCD(int first, int second){        // 0 is divisible by every number        if(first == 0)        {               return second;        }        if(second == 0)        {               return first;        }        // both numbers are equal        if(first == second)        {           return first;        }        // first is greater        else if(first > second)        {           return findGCD(first - second, second);        }        return findGCD(first, second - first);}`

C++ Program to find HCF or GCD of two numbers

### C++ code:

`//C++ Program//GCD of Two Numbers#include<iostream>using namespace std;// Recursive function declarationint findGCD(int, int);// main programint main(){        int first, second;        cout<<"Enter First Number: ";        cin>>first;        cout<<"Enter second Number: ";        cin>>second;        cout<<"GCD of "<<first<<" and "<<second<<" is "<<findGCD(first,second);        return 0;}//body of the functionint findGCD(int first, int second){        // 0 is divisible by every number        if(first == 0)        {               return second;        }        if(second == 0)        {               return first;        }        // both numbers are equal        if(first == second)        {           return first;        }        // first is greater        else if(first > second)        {           return findGCD(first - second, second);        }        return findGCD(first, second - first);}`

Java Program to find HCF or GCD of two numbers

Code is not available yet, comment down the code below the page in the comment section using the same logic or different approach you want to.

### 4 comments on “GCD of Two Numbers asked in Wipro NLTH”

• Rohit

import math
print(math.gcd(23,34)) 1
• Anurag Raj

def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)
num1 = int(input(“Enter first number:”))
num2 = int(input(“Enter second number:”))
print (gcd(num1,num2)) 0
• Satyam Pandey

// GCD of 2 no. by looping –
def computeGCD(x, y):
if x > y:
small = y
else:
small = x
for i in range(1, small+1):
if((x % i == 0) and (y % i == 0)):
gcd = i
return gcd

print (“The gcd of”,x,”and”,y,”is : “,end=””)

print (computeGCD(60,48)) 0
• Satyam Pandey

import math

print (“The gcd of 60 and 48 is : “,end=””)

print (math.gcd(60,48)) 1