Intro to Java Chapter 05 Exercise 14 - Compute the Greatest Common Divisor

preview_player
Показать описание

Book: Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition
Authors: Daniel Liang
Chapter: 05 Loops
Programming Exercise: 14 - Compute the Greatest Common Divisor
Programming Language: Java
IDE: Intellij
Theme: Darkest Dark
Recording Software: CyberLink Screen Recorder

Feel free to leave comments and suggestions on how to make these videos better.
Рекомендации по теме
Комментарии
Автор

you could also use this condition in the while loop
(number1 % gcd != 0 || number2 % gcd != 0)

a.m.e.e.e.r
Автор

can you please extend your explain on it? how the program goes into the while condition, cz we also have !(number2 % gcd == 0), it means that always false

olivertauk
Автор

thank me later





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);
}
}

sksk-bfjs