Closed letientai299 closed 6 years ago
__Need Help___ I want to input number in gdc function ,can you help me please .
package com.mycompany.app;
import java.util.Scanner;
/**
* Created by thien on 01/08/2017.
*/
public class GreatestCommonDivisior {
static int gcd(int a, int... numbers) {
int result = a;
for (int number : numbers) {
result = findGcd(result, number);
}
return result;
}
static int findGcd(int a, int b) {
if (a < 0) {
a = Math.abs(a);
}
b = Math.abs(b);
if (a == 0) {
return b;
}
return findGcd(b % a, a);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("insert n number");
int n = sc.nextInt();
int num[]=new int[n];
for (int i=0;i<num.length;i++) {
System.out.println("enter a number");
num[i] = sc.nextInt();
gcd(num[i])
}
}
}
You can do something like:
gcd(num[0], rest); // rest is an int[] that contains n-1 values from num
Problem
Write a function that compute GCD of 2 or more numbers.
Expectation
Some example:
Hint
gcd
method should accept 2 or more numbers, you will need to know how to use java varags to define such a function.