Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // program to find the gcd of 2 numbers using euclids algorithm
- import java.util.Scanner;
- public class findGcd{
- public static void main(String arg[]){
- boolean cont = true;
- int number1, number2, answer;
- Scanner sc = new Scanner(System.in);
- while(cont){
- System.out.print("\nEnter the first number : ");
- number1 = sc.nextInt();
- System.out.print("Enter the second number : ");
- number2 = sc.nextInt();
- answer = gcd(number1, number2);
- System.out.println("The gcd of " + number1 + " and " + number2 + " is : " + answer);
- System.out.print("\nDo you want to continue : ");
- int n = sc.nextInt();
- if(n == 0){
- cont = false;
- }
- }
- }
- static int gcd(int num1, int num2){
- int temp,quotient;
- if(num1>num2)
- {
- do
- {
- quotient = num1 / num2;
- temp = num2;
- num2 = num1 - quotient * num2;
- num1 = temp;
- }while(num2!=0);
- return num1;
- }
- else
- {
- do
- {
- quotient = num2 / num1;
- temp = num1;
- num1 = num2 - quotient * num1;
- num2 = temp;
- }while(num1!=0);
- return num2;
- }
- }
- }
Add Comment
Please, Sign In to add comment