Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class GCD {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. System.out.print("Please enter a number: ");
  7. int a = scan.nextInt();
  8. System.out.print("Please enter a number: ");
  9. int b = scan.nextInt();
  10. System.out.println(gcd(a, b));
  11. }
  12.  
  13. public static int gcd(int a, int b) {
  14. if (a < b) {
  15. int c = b;
  16. b = a;
  17. a = c;
  18. }
  19. while (a != 0 && b != 0) {
  20. int c = b;
  21. b = b % a;
  22. a = c;
  23. }
  24. return a + b;
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement