Docsity

Java GDC - Complete Code, Cheat Sheet of Java Programming

Toggle sidebar
bg1
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
// Calculate GCD using Euclidean algorithm
while (num1 != num2) {
if (num1 > num2) {
num1 -= num2;
} else {
num2 -= num1;
}
}
System.out.println("The GCD of the two numbers is: " + num1);
scanner.close();
}
}

Partial preview of the text

Download Java GDC - Complete Code and more Cheat Sheet Java Programming in PDF only on Docsity!

import java.util.Scanner; public class GCD { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); // Calculate GCD using Euclidean algorithm while (num1 != num2) { if (num1 > num2) { num1 -= num2; } else { num2 -= num1; } } System.out.println("The GCD of the two numbers is: " + num1); scanner.close(); } }