0
2.2kviews
Write a program to calculate the GCD of numbers in JAVA.

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 5M

Year: May 2015

1
 import java.util.*;
    class GCD{
            public static void main(String arg[]){
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER TWO NUMBERS :");
        int a=sc.nextInt();
        int b=sc.nextInt();
        int large,small,r;
        if(a>b){     //finding the larger number 
            large=a;
            small=b;
        }
        else {
            large=b;
            small=a;
        }
        while(small!=0){                   //this method is called Successive divison method.  
            r=large%small;            //eg. : large=24, small =18, i.e.  r=6  
            large=small;                // large= 18 
            small=r;                       // small=6
            }
        System.out.println("GCD IS : "+large);
    }
    }
    OUTPUT: 
    ENTER TWO NUMBERS
    100
    50
    GCD IS : 50

1 Answer
0
8views
Import java.io.*;
public class GCDExample { 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));
}
Please log in to add an answer.