Users Online
· Guests Online: 31
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C Program to find HCF of a given Number using Recursion
The following C program using recursion finds the HCF of two entered integers. The HCF stands for Highest Common Factor.
Here is the source code of the C program to find HCF of two numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to find HCF of a given Number using Recursion
*/
#include <stdio.h>
int hcf(int, int);
int main()
{
int a, b, result;
printf("Enter the two numbers to find their HCF: ");
scanf("%d%d", &a, &b);
result = hcf(a, b);
printf("The HCF of %d and %d is %d.\n", a, b, result);
}
int hcf(int a, int b)
{
while (a != b)
{
if (a > b)
{
return hcf(a - b, b);
}
else
{
return hcf(a, b - a);
}
}
return a;
}
$ cc pgm32.c
$ a.out
Enter the two numbers to find their HCF: 24 36
The HCF of 24 and 36 is 12.
Here is the source code of the C program to find HCF of two numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to find HCF of a given Number using Recursion
*/
#include <stdio.h>
int hcf(int, int);
int main()
{
int a, b, result;
printf("Enter the two numbers to find their HCF: ");
scanf("%d%d", &a, &b);
result = hcf(a, b);
printf("The HCF of %d and %d is %d.\n", a, b, result);
}
int hcf(int a, int b)
{
while (a != b)
{
if (a > b)
{
return hcf(a - b, b);
}
else
{
return hcf(a, b - a);
}
}
return a;
}
$ cc pgm32.c
$ a.out
Enter the two numbers to find their HCF: 24 36
The HCF of 24 and 36 is 12.
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.