Adsense 728 * 15


Custom Search

Find GCD of two Numbers using Recursion in C, C++

Write a C, C++ program to find GCD, HCF of two numbers. Before solving this problem let's understand what is GCD or HCF.

What is GCD (Greatest Common Divisior) or HCF (Highest Common Factor) ?

GCD or HCF of two  numbers is the largest number which divides both the number without leaving any remainder.

Binary Search Algorithm .

Let's take an example - Take two numbers 63 and 21.

Divisible of 63 is 3, 7, 9, 21 ,63

Divisible of 21 is 3, 7, 21

Highest Divisible of both the numbers without leaving any remainder is 21.

Check whether number is Armstrong or not.

GCD of Two Numbers - C Program

#include <stdio.h>

int gcd (int a, int b) {

      /* If b is equal to zero. */

      if(b == 0) {
          return a;
      // Recursive call

int main(void) {

 int a,b,g;
 printf("Enter two numbers \n");
 scanf("%d %d",&a,&b);
 g = gcd(a,b);
 printf("GCD of two number %d %d is %d",a,b,g);
 return 0;

Output :

Enter two numbers  63  21

GCD of two number 63 21 is 21

No comments:

Post a Comment