C Program to Find HCF of Two Numbers without Recursion

The following C program using iteration 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 the HCF of two entered integers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to find HCF of a given Number without using Recursion
  3.  */
  4. #include <stdio.h>
  5.  
  6. int hcf(int, int);
  7.  
  8. int main()
  9. {
  10.     int a, b, result;
  11.  
  12.     printf("Enter the two numbers to find their HCF: ");
  13.     scanf("%d%d", &a, &b);
  14.     result = hcf(a, b);
  15.     printf("The HCF of %d and %d is %d.\n", a, b, result);
  16.  
  17.     return 0;
  18. }
  19.  
  20. int hcf(int a, int b)
  21. {
  22.     while (a != b)
  23.     {
  24.         if (a > b)
  25.         {
  26.             a = a - b;
  27.         }
  28.         else
  29.         {
  30.             b = b - a;
  31.         }
  32.     }
  33.     return a;
  34. }

$ cc pgm31.c
$ a.out
Enter the two numbers to find their HCF: 24 36
The HCF of 24 and 36 is 12.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.