GREATEST COMMON DIVISOR

 

  1. package com.cnc;

    public class Main {

    public static void main(String[] args) {
    System.out.println(getGreatestCommonDivisor(15,12));
    }


    public static int getGreatestCommonDivisor(int first, int second){

    int divisor = 0;

    if ((first < 10) || (second < 10)){
    return -1;
    }

    if (first > second){
    for (int i=2; i < first; i++){
    if ((first % i == 0) && (second % i == 0)){
    divisor = i;
    }
    }
    }

    if (first < second){
    for (int i=2; i < second; i++){
    if ((first % i == 0) && (second % i == 0)){
    divisor = i;
    }
    }
    }
    return divisor;
    }
    }


Popular posts from this blog

NUMBER PALINDROME CHALLENGE

ENCAPSULATION CHALLENGE

LARGEST PRIME FACTOR OF A NUMBER