On Mac OS X
Created with Java Version 8 (Update 73)


Save the program - GCD.class

Open the terminal app in Applications/Utilities

Go to the folder where you saved it using the cd (Change Directory) command
e.g. "cd Downloads"
type "/usr/bin/java GCD"

In Windows 10 :
Save the program - GCD.class In Edge, IE or Firefox click on it and select save or keep. In Chrome right click and select "Save link as" Press the windows logo key +R to get the run or search box or select the "Ask me anything" box on the bottom left. Type "cmd.exe" (or just "cmd"). Go to the command processor/terminal/prompt/shell window you just opened. Go to the folder you specified for downloads (Downloads is the default). e.g. "cd C:\Users\User_name\Downloads" (change directory [cd] to Downloads) Type "java GCD" (Do not type "GCD.class")

Complete Java Code
import java.util.Scanner;

 class GCD { 
  public static void main(String args[]){ 
  //Enter two number whose GCD needs to be calculated.
   Scanner scanner = new Scanner(System.in);
   System.out.println("Please enter first number to find GCD");
   int number1 = scanner.nextInt();
   System.out.println("Please enter second number to find GCD");
   int number2 = scanner.nextInt();
   
   System.out.println("GCD of two numbers " + number1 
       +" and " + number2 +" is :" + findGCD(number1,number2));
  }
    
     /*
     * Java method to find GCD of two number using Euclid's method 
     * @return GDC of two numbers in Java
     */
  private static int findGCD(int number1, int number2) {
   //base case
    if(number2 == 0){
     return number1;
     }
     return findGCD(number2, number1%number2);
   }
}

Example from Java67

last updated 4 May 2016