C program to find HCF.

 

01 #include<stdio.h>
02
03 int hcf(int,int);
04
05 int main()
06 {
07
08 int x,y=-1;
09
10 printf("Enter numbers to find HCF/GCD (press 0 to exit) : ");
11
12 while(1)
13 {
14 scanf("%d",&x);
15
16 if(x<1)
17 break;
18 else if(y==-1)
19 y=x;
20 else if (x<y)
21 y=hcf(x,y);
22 else
23 y=hcf(y,x);
24 }
25
26 printf("HCF/GCD is %d",y);
27 return 0;
28 }
29
30 int hcf(int x,int y)
31 {
32 int i;
33
34 for(i=x; i>=1; i--)
35 {
36 if(x%i==0&&y%i==0)
37 {
38 break;
39 }
40 }
41 return i;
42 }
 
 
OUTPUT :
Enter numbers to find HCF/GCD (press 0 to exit) :

3
6
9
0

HCF/GCD is 3

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.