This program calculates the greatest common divisor (gcd) and the least common multiple (lcm) of two numbers.
Here I have to say that this program is quite slow (because the algorithm is simply bad) and the calculation of large numbers takes a long time. On the internet there are much better and faster programs with better algorithms.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.*; import java.util.Scanner; class Testprogram { public static void main(String [] args) { long eingabe1, eingabe2, zahl1, zahl2, ggT; System.out.println(" "); System.out.println("Please enter two numbers which lcm and gcd should be determined!"); Scanner eingabe = new Scanner(System.in); eingabe1 = Long.valueOf(eingabe.next()); eingabe2 = Long.valueOf(eingabe.next()); if(eingabe1 == 0 || eingabe2 == 0) { System.out.println("None of the entered numbers can be zero!"); } else { for(zahl1 = eingabe1; zahl1 <= eingabe1*eingabe2; zahl1 = zahl1 + eingabe1) { for(zahl2 = eingabe2; zahl2 <= eingabe1*eingabe2; zahl2 = zahl2 + eingabe2) { if(zahl1 == zahl2){ ggT = (eingabe1*eingabe2)/zahl1; System.out.println("The least common multiple of " + eingabe1 + " and " + eingabe2 + " is " + zahl1); System.out.println("The greatest common divisor of " + eingabe1 + " and " + eingabe2+" is " + ggT); System.exit(0); } } } } }