DEV Community

Cover image for Basic Programs in Python,Java & JavaScript: Smallest Number, Prime Number, and GCD
Harini
Harini

Posted on

Basic Programs in Python,Java & JavaScript: Smallest Number, Prime Number, and GCD

Finding the Smallest Number Among Three Using Ternary Operator

What is a Ternary Operator?

A ternary operator is a shorthand way of writing an if-else condition in a single line.

Syntax:

condition ? value_if_true : value_if_false;

JavaScript Code

let a = 5, b = 2, c = 8;

let smallest = (a<b && a<c) ? a : ((b<c) ? b : c)
console.log("Smallest number is: ")
Enter fullscreen mode Exit fullscreen mode

Python Code

a = int(input("Enter number 1:"))
b = int(input("Enter number 2:"))
c = int(input("Enter number 3:"))

smallest = a if (a < b and a < c) else (b if b < c else c)
print("Smallest number is:", smallest)
Enter fullscreen mode Exit fullscreen mode

Java Code

public class SmallestNumber {
    public static void main(String[] args) {

        int a = 5, b = 2, c = 8;

        int smallest = (a < b && a < c) ? a : ((b < c) ? b : c);

        System.out.println("Smallest number is: " + smallest);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

How to find prime numbers within a given range

A prime number is a number greater than 1 that has only two factors:
1 and itself

Examples:
2, 3, 5, 7, 11...

Python Code:

no = 10
end =  50

while no <= end :
    div = 2
    flag = True

    while div <= no/2:
        if no % div == 0:
            flag = False
            break
        div = div + 1
    if flag and no > 1:
        print(no, "is Prime")
    no = no + 1
Enter fullscreen mode Exit fullscreen mode

JavaScript Code

let no = 10;
let end = 50;

while (no <= end) {
    let div = 2;
    let flag = true;

    while (div <= no / 2) {
        if (no % div === 0) {
            flag = false;
            break;
        }
        div = div + 1;
    }

    if (flag && no > 1) {
        console.log(no + " is Prime");
    }

    no = no + 1;
}
Enter fullscreen mode Exit fullscreen mode

Java Code

public class PrimeNumbers {
    public static void main(String[] args) {

        int no = 10;
        int end = 50;

        while (no <= end) {
            int div = 2;
            boolean flag = true;

            while (div <= no / 2) {
                if (no % div == 0) {
                    flag = false;
                    break;
                }
                div = div + 1;
            }

            if (flag && no > 1) {
                System.out.println(no + " is Prime");
            }

            no = no + 1;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

How to find GCD using a simple looping approach

The Greatest Common Divisor (GCD) is the largest number that divides two or more numbers without leaving a remainder.

For example:
GCD of 10 and 20 is 10

Python Code

no1 = 10
no2 = 20

small = no1 if no1 < no2 else no2
gcd = 0
div = 2

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

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

JavaScript Code

let no1 = 10;
let no2 = 20;

let small = (no1 < no2) ? no1 : no2;
let gcd = 0;
let div = 2;

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

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

Java Code

public class GCDExample {
    public static void main(String[] args) {

        int no1 = 10;
        int no2 = 20;

        int small = (no1 < no2) ? no1 : no2;
        int gcd = 0;
        int div = 2;

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

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

Output

Top comments (0)