DEV Community

S Sarumathi
S Sarumathi

Posted on

Greatest Common Divisor

2.Gcd:
Flowchart:

Javascript:
Program:

let no1 = 10;
let no2 = 20;

let small = (no1 < no2) ? no1 : no2;
let gcd = 1;

let div = 2;

while (div <= small) {
    if (no1 % div === 0 && no2 % div === 0) {
        gcd = div;
    }
    div++;
}

console.log("GCD:", gcd);
Enter fullscreen mode Exit fullscreen mode

Output:

Java:
Program:

public class PrimeRange {
    public static void main(String[] args) {
int no1 = 10;
int no2 = 20;

int small = (no1 < no2) ? no1 : no2;
int  gcd = 1;

int div = 2;

while (div <= small) {
    if (no1 % div == 0 && no2 % div == 0) {
        gcd = div;
    }
    div++;
}

   System.out.println("GCD:"+ gcd);
}
}
Enter fullscreen mode Exit fullscreen mode

Python:
Program:

no1 = 10
no2 = 20

small = no1 if no1 < no2 else no2
gcd = 1

div = 2

while div <= small:
    if no1 % div == 0 and no2 % div == 0:
        gcd = div
    div += 1

print("GCD:", gcd)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)