Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package whatever; // don't place package name! */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- /* Name of the class has to be "Main" only if the class is public. */
- class Ideone
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- // your code goes here
- System.out.println(gcd(9, 4));
- System.out.println(gcdNums(new int[]{2, 4, 6, 9}));
- }
- public static int gcdNums(int[] nums) {
- int result = nums[0];
- for (int i = 1; i < nums.length; i++) {
- result = gcd(result, nums[i]);
- }
- return result;
- }
- public static int gcd(int a, int b) {
- while (a != b) {
- if (a > b) {
- a = a - b;
- } else {
- b = b - a;
- }
- }
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement