
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Java GDC code start to finish - working.
Typology: Cheat Sheet
1 / 1
This page cannot be seen from the preview
Don't miss anything!

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(); } }