DEV Community

Keerthiga P
Keerthiga P

Posted on

Sum of Numbers,Factorial,LCM

1.Sum of n numbers:

FlowChart:

JavaScript:

let sum = 0;
let num = 1;
while (num <= 10) {
    sum = sum + num;
    num = num + 1;
}
console.log("Sum =", sum);
Enter fullscreen mode Exit fullscreen mode

Output:

Java:

public class Main {
    public static void main(String[] args) {
        int sum = 0;
        int num = 1;
        while (num <= 10) {
            sum = sum + num;
            num = num + 1;
        }
        System.out.println("Sum = " + sum);
    }
}
Enter fullscreen mode Exit fullscreen mode

Python:

sum = 0
num = 1
while num <= 10:
    sum = sum + num
    num = num + 1
print("Sum =", sum)
Enter fullscreen mode Exit fullscreen mode

2.Factorial of n Numbers:

FlowChart:

Javascript:

let fac = 1;
let no = 1;
while (no <= 10) {
    fac = fac * no;
    no = no + 1;
}
console.log("Factorial =", fac);
Enter fullscreen mode Exit fullscreen mode

Output:

Java:

public class Main {
    public static void main(String[] args) {
        int fac = 1;
        int no = 1;
        while (no <= 10) {
            fac = fac * no;
            no = no + 1;
        }
        System.out.println("Factorial = " + fac);
    }
}
Enter fullscreen mode Exit fullscreen mode

Python:

fac = 1
no = 1
while no <= 5:
    fac = fac * no
    no = no + 1
print("Factorial =", fac)
Enter fullscreen mode Exit fullscreen mode

3.LCM(Least Common Multiple):

Flowchart:

Javascript:

let no1 = 3;
let no2 = 10;
let big = no1 > no2 ? no1 : no2;
let big2 = big;
while (true) {
    if (big % no1 === 0 && big % no2 === 0) {
        console.log("LCM =", big);
        break;
    }
    big += big2;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Java:

public class LCMExample {
    public static void main(String[] args) {
        int no1 = 3;
        int no2 = 10;
        int big = (no1 > no2) ? no1 : no2;
        int big2 = big;

        while (true) {
            if (big % no1 == 0 && big % no2 == 0) {
                System.out.println("LCM = " + big);
                break;
            }
            big += big2;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Python:

no1 = 3
no2 = 10
big = no1 if no1 > no2 else no2
big2 = big
while True:
    if big % no1 == 0 and big % no2 == 0:
        print("LCM =", big)
        break
    big += big2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)