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 declaration
int findGCD(int, int);
// main program
int 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 function
int 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 declaration
int findGCD(int, int);
// main program
int 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 function
int 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.

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

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


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


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