Advertisement
jaVer404

level14.lesson08.bonus02

Jun 25th, 2015
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package com.javarush.test.level14.lesson08.bonus02;
  2.  
  3. /* НОД
  4. Наибольший общий делитель (НОД).
  5. Ввести с клавиатуры 2 целых положительных числа.
  6. Вывести в консоль наибольший общий делитель.
  7. */
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12.  
  13. public class Solution
  14. {
  15.     public static void main(String[] args) throws Exception
  16.     {
  17.         System.out.println(NOD());
  18.     }
  19.     private static int NOD () throws IOException {
  20.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  21.         int chislo1 = Integer.parseInt(reader.readLine());
  22.         int chislo2 = Integer.parseInt(reader.readLine());
  23.         int nod;
  24.         if (chislo1>chislo2) {
  25.             nod = chislo2;
  26.         }
  27.         else {
  28.             nod = chislo1;
  29.         }
  30.         while (true) {
  31.             if ((chislo1%nod == 0)&& chislo2%nod == 0) {
  32.                 break;
  33.             }
  34.             else
  35.                 nod--;
  36.         }
  37.         return nod;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement