Find GCD of Two number in Java - Euclidean algorithm - 2024
Finding GCD of Two Number First we need to modules the first number by second number, then store the output in second number. Before doing the modules store the second number value for next iteration in temp variable. And do the process still the second number becomes zero. Code : package com.dinatechy.challange; import java.util.Scanner; public class GCDofNumber { 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(); scanner .close(); // Calculate the GCD int gcd = findGCD ( num1 , num2 ); // Print the result System. out .println( "The GCD of " + num1 + " and " + num2 + " is: " + gcd ); } // Method to find the GCD usi...