Problem:
Design an algorithm and implement that will find the Greatest Common Divisor of n positive non-zero integers
Solution:
package com.myprograms;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class GCDOfNNumbers {
int n;
List<Integer> list = new ArrayList<Integer>();
int smallNumber;
int largerNumber;
int result;
int input1;
int input2;
public static void main(String[] args) {
GCDOfNNumbers gcdOfTwoNumbers = new GCDOfNNumbers();
gcdOfTwoNumbers.getTheNumbers();
gcdOfTwoNumbers.findGCD();
gcdOfTwoNumbers.printTheResult();
}
public void getTheNumbers(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the n value");
n = scanner.nextInt();
for(int i = 0;i<n;i++){
System.out.println("enter number");
list.add(scanner.nextInt());
}
Collections.sort(list);
scanner.close();
}
public void findGCD(){
int reminder = 0;
int res = 0;
for(int i = 0; i< n - 1; i++){
reminder = list.get(i + 1) % list.get(i);
while(reminder != 0){
list.set(i+1, list.get(i));
list.set(i, reminder);
reminder = list.get(i + 1) % list.get(i);
res = list.get(i);
}
}
result = res;
}
public void printTheResult(){
System.out.println("the Greatest Common Divisor of given numbers is: " + result);
}
}
Output:
enter the n value
4
enter number
2
enter number
4
enter number
6
enter number
8
the Greatest Common Divisor of given numbers is: 2
Design an algorithm and implement that will find the Greatest Common Divisor of n positive non-zero integers
Solution:
package com.myprograms;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class GCDOfNNumbers {
int n;
List<Integer> list = new ArrayList<Integer>();
int smallNumber;
int largerNumber;
int result;
int input1;
int input2;
public static void main(String[] args) {
GCDOfNNumbers gcdOfTwoNumbers = new GCDOfNNumbers();
gcdOfTwoNumbers.getTheNumbers();
gcdOfTwoNumbers.findGCD();
gcdOfTwoNumbers.printTheResult();
}
public void getTheNumbers(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the n value");
n = scanner.nextInt();
for(int i = 0;i<n;i++){
System.out.println("enter number");
list.add(scanner.nextInt());
}
Collections.sort(list);
scanner.close();
}
public void findGCD(){
int reminder = 0;
int res = 0;
for(int i = 0; i< n - 1; i++){
reminder = list.get(i + 1) % list.get(i);
while(reminder != 0){
list.set(i+1, list.get(i));
list.set(i, reminder);
reminder = list.get(i + 1) % list.get(i);
res = list.get(i);
}
}
result = res;
}
public void printTheResult(){
System.out.println("the Greatest Common Divisor of given numbers is: " + result);
}
}
Output:
enter the n value
4
enter number
2
enter number
4
enter number
6
enter number
8
the Greatest Common Divisor of given numbers is: 2
enter the n value
4
enter number
35
enter number
95
enter number
215
enter number
65
the Greatest Common Divisor of given numbers is: 5
enter the n value
5
enter number
221
enter number
39
enter number
4199
enter number
247
enter number
91
the Greatest Common Divisor of given numbers is: 13
No comments:
Post a Comment