HCF Of Two Numbers

Write a  C program to find  HCF of Two Numbers

The HCF or the Highest Common Factor of two numbers is the largest common factor of two or more values. The HCF can be calculated using some simple mathematical tricks. The following algorithm will determine how a c program can calculate the HCF of two numbers.

Working :-

Step 1. Start

Step 2. Define variables P and Q

Step 3. Develop a loop from 1 to the maximum value of P and Q.

Step 4. Check if both P and Q are completely divided by the same loop, if it does, store the number.

Step 5. Print the stored number as HCF.

Step 6. Stop

GCD or HCF of two numbers in C programming (1)

C Program to calculate HCF of two numbers

#include <stdio.h> 
int main()
{
//for initialize variables int a, b, i, hcf; a = 12; b = 16; //find hcf of number for(i = 1; i <= a || i <= b; i++)
{ if( a%i == 0 && b%i == 0 ) hcf = i; }
//display hcf printf("HCF = %d", hcf); return 0; }

Output

HCF = 4

Please Login/Signup to comment