DEV Community

Sasireka
Sasireka

Posted on

Ternary Operator, GCD, Prime Numbers

1)Ternary Operator

  • A ternary operator is a shorthand conditional operator (
    condition ? exprIfTrue : exprIfFalse) that evaluates a condition and returns one of two values based on whether the result is true or false, acting as a concise replacement for simple if-else statements.

  • Python

a = 10
b = 5
c = 8

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

Output

  • Javascript
let a = 100;
let b = 55;
let c = 67;

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

console.log("Smallest number is:", smallest);
Enter fullscreen mode Exit fullscreen mode

Output

  • Java
public class SmallestOfThree {
    public static void main(String[] args) {
        int a = 55;
        int b = 99;
        int c = 32;

        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

2)Range of Prime Number

  • Flowchart

  • Python
no1 = 10

while no1 <= 50:
    div = 2
    flag = True

    while div <= no1 // 2:
        if no1 % div == 0:
            flag = False
            break
        div = div + 1

    if flag:
        print(no1, "is prime")

    no1 = no1 + 1
Enter fullscreen mode Exit fullscreen mode

Output

  • Javascript
let no1 = 10;

while (no1 <= 50) {
    let div = 2;
    let flag = true;

    while (div * div <= no1) {
        if (no1 % div === 0) {
            flag = false;
            break;
        }
        div++;
    }

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

    no1++;
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Java
public class PrimeCheck {
    public static void main(String[] args) {

        int no1 = 10;

        while (no1 <= 50) {
            int div = 2;
            boolean flag = true;

            while (div * div <= no1) {
                if (no1 % div == 0) {
                    flag = false;
                    break;
                }
                div++;
            }

            if (flag && no1 > 1) {
                System.out.println(no1 + " is prime");
            } else {
                System.out.println(no1 + " is not prime");
            }

            no1++;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

3)Greatest Common Divisor

  • Flowchart

  • Python
no1 = 10
no2 = 20

small = no1 if no1 < no2 else no2

GCD = 1
div = 1

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

print(GCD)

Enter fullscreen mode Exit fullscreen mode

Output

  • Javascript
let no1 = 10;
let no2 = 20;

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

let gcd = 1;
let div = 1;

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

console.log("GCD =", gcd);

Enter fullscreen mode Exit fullscreen mode
  • Java
public class GCD {
    public static void main(String[] args) {

        int no1 = 10;
        int no2 = 20;

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

        int gcd = 1;
        int div = 1;

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

        System.out.println("GCD = " + gcd);
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)