同时给出两个数字的hcf和gcd的程序

时间:2013-10-28 03:54:19

标签: java

我是初学者,这是为了学校。请帮我这个,它必须是一个非常简单的程序,用户输入两个数字,结果是两个数字,一个hcf和一个gcd。 我分开编写了代码,但我不知道如何将它们组合起来。

1 个答案:

答案 0 :(得分:2)

转到GCD程序并获取计算GCD的代码并将其移动到函数中:

public int GCD(int a, int b) {
    //find GCD
    //return GCD
}

为HCF做同样的事情:

public int HCF(int a, int b) {
    //find HCF
    //return HCF
}

然后在主要方法中:

//all the code for prompting the user for input
//and all the code for asking the user for input
//code which you've already written if you wrote these two programs independently already
System.out.println("GCD of " + input1 + " and " + input2 + " is " + gcd(input1, input2));
System.out.println("HCF of " + input1 + " and " + input2 + " is " + hcf(input1, input2));