DEV Community

Cover image for Number Logic in Programming: Prime Numbers, GCD, and Ternary Operators
Arul .A
Arul .A

Posted on

Number Logic in Programming: Prime Numbers, GCD, and Ternary Operators

1.find the range of prime numbers :

Flow Chart:

-In 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:

-In 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");
    } else {
        console.log(no1 + " is not prime");
    }

    no1++;
}
Enter fullscreen mode Exit fullscreen mode

-In 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

2.Find the Greatest of Common Divisor:

Flow Chart:

-In 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:

-In 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
  • In 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

3.Ternary Operator for 3 numbers :

-In python :

a = 10
b = 5
c = 8

small = a if a < b and a < c else (b if b < c else c)

print("Smallest =", small)

Enter fullscreen mode Exit fullscreen mode
  • In Javascript:
let a = 10;
let b = 5;
let c = 8;

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

console.log("Smallest =", small);


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

        int a = 10;
        int b = 5;
        int c = 8;

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

        System.out.println("Smallest = " + small);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)