Example & Tutorial understanding programming in easy ways.

Write a program to find the HCF of two number

HCF:
Stand for Highest common factor.

Example-
HCF of 8 and 12 is 4
HCF of 5 and 7 is 1
HCF of 5 ad 10 is 5

program-

#include< stdio.h>
int main() {
int i,n1,n2,min;
printf("Enter first number");
scanf("%d",&n1);
printf("Enter second number");
scanf("%d",&n2);
if(n1< n2)
min=n1;
else
min=n2;
for(i=min;i>0;i--)
{
if(n1%i==0 && n2%i==0)
{
printf("HCF is %d",i);
break;
}
}
}


output-

Enter first number
10
Enter second number
25
HCF is 5

Enter first number
5
Enter second number
6
HCF is 1

-In this program, we take an input of two integer number and try to find there HCF.
-We run loop starting from minimum number and goes to 1.
-In between, if any value that is divide both input value, then it is considered as HCF of both number so we break the loop and display the HCF.




Read More →
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!