In this program user ask to find out the HCF (highest common factor) using function. As declaring required variable user ask to enter the value. User made a function named “HCF”. Then using control statement if(a>b) the HCF (a,b) else (b,a) after fetching the HCF through getch().
In the next move user calls the function that made HCF. Here user also declare a variable named r=1. And a while condition too. While(r! =0) if the condition got true than the value must be fetch out from use of modulus operator. After that interchange the value within x, y or r. display the result on the screen.
Problem Statement:
This is C program that asks user to find out the HCF using function.
- Declaring the variable.
- Using control statement.
- Display result on the screen.
This is C program here user need to find out the HCF using function Method. Output of this program shown below.
#include<stdio.h>
int hcf(int x,int y);
void main()
{
int a,b,d;
clrscr();
printf("Enter 2 Numbers : ");
scanf("%d%d",&a,&b);
if(a>b)
{
d=hcf(a,b);
}
else
{
d= hcf(b,a);
}
printf("HCF is= %d",d);
getch();
}
int hcf(int x,int y)
{
int r=1;
while(r!=0)
{
r=x%y;
x=y;
y=r;
}
return(x);
}