Filled Under: ,

C program to find GCD of two or three numbers using function

 C program to find GCD of two or three numbers using function

GCD of two or three numbers using function


 In this program, I have discussed about how to find the GCD of two or three number using function in the C programming language.


The full form of GCD is greatest common divisor. If we consider three number 10,15,20 then the GCD of these three number will be 5.


int hcf2(int,int);
int hcf3(int,int,int);
 int main()
{ int a,b,c,d,hc1,hc2,n;
printf("Enter the number of numbers whose HCF you want to calculate (2<=number<=3) \n");
scanf("%d",&n);
switch(n)
{ case 2:
printf("Enter two numbers \n");
scanf("%d%d",&a,&b);
hc1=hcf2(a,b);
printf("The HCF of %d and %d is %d \n",a,b,hc1);
break;
case 3:
printf("Enter three numbers \n");
scanf("%d%d%d",&a,&b,&c);
hc2=hcf3(a,b,c);
printf("The HCF of %d, %d and %d is %d \n",a,b,c,hc2);
break;
}
}
int hcf2( int k1,int k2)
{
int i,h1=0;
 for(i=1;i<=k1;i++)
{
if(k1%i==0&&k2%i==0)
{
if(i>h1)
{h1=i;
}
}
}
return h1;
}
int hcf3( int k1,int k2,int k3)
{int i,h2=0;
 for(i=1;i<=k1;i++)
{
if(k1%i==0&&k2%i==0&&k3%i==0)
{
if(i>h2)
{h2=i;
}
}
}
return h2;
}


Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.

Enjoy programming in C.

0 comments:

Post a Comment

 

Copyright 2020 Programming in C All Right Reserved