Sudeep Kumar Das

Write a function that calculates the Greatest Common Divisor of 2 numbers.

Java Basic Codes

Explanation

1269    0

import java.util.*;

 public class Solutions {

   public static void main(String args[]) {

       Scanner sc = new Scanner(System.in);

       int n1 = sc.nextInt();

       int n2 = sc.nextInt();

 

       while(n1 != n2) { 

           if(n1>n2) {

               n1 = n1 - n2;

           } else {

               n2 = n2 - n1;

           }

       }

       System.out.println("GCD is : "+ n2);

   }   

}



Share:   

More Questions from Java Basic Codes Module 0