H.C.F

优质
小牛编辑
135浏览
2023-12-01

HCF或最高公因子是两个或更多值的最大公因子。

For example ,12和16的因素是 -

12→1,2,3,4,6,12

16→1,2,4,8,16

常见因素是1,2,4,最高公因子是4。

算法 (Algorithm)

该程序的算法可以推导为 -

START
   Step 1 → Define two variables - A, B
   Step 2 → Set loop from 1 to max of A, B
   Step 3 → Check if both are completely divided by same loop number, if yes, store it
   Step 4 → Display the stored number is HCF
STOP

伪代码 (Pseudocode)

procedure even_odd()
   Define two variables a and b
   FOR i = 1 TO MAX(a, b) DO
      IF a % i is 0 AND b % i is 0 THEN
         HCF = i
      ENDIF
   ENDFOR
   DISPLAY HCF
end procedure

实现 (Implementation)

该算法的实现如下 -

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

输出 (Output)

该方案的产出应该是 -

HCF = 4