gcd of multiple numbers code example

Example 1: gcd of two numbers c++

// gcd function definition below:
int gcd(int a, int b) {
   if (b == 0)
   return a;
   return gcd(b, a % b);
}

int a = 105, b = 30;
cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b);
// output = "GCD of 105 and 30 is 15";

Example 2: gcd of two numbers in c

#include <stdio.h>
int main()
{
    int n1, n2, i, gcd;

    printf("Enter two integers: ");
    scanf("%d %d", &n1, &n2);

    for(i=1; i <= n1 && i <= n2; ++i)
    {
        // Checks if i is factor of both integers
        if(n1%i==0 && n2%i==0)
            gcd = i;
    }

    printf("G.C.D of %d and %d is %d", n1, n2, gcd);

    return 0;
}

Example 3: c++ program to find gcd of 3 numbers

#include<stdio.h>
int main() {    
  int a,b,c,hcf,st;
  printf("Enter three numbers : ");
  scanf("%d,%d,%d", &a,&b,&c);
  st=a<b?(a<c?a:c):(b<c?b:c);
  for (hcf=st;hcf>=1;hcf--) 	{  	  
    if (a%hcf==0 && b%hcf==0 && c%hcf==0)
      break;
  }
  printf("%d",hcf); return 0;
}