DEV Community

Cover image for LCM, Sum of N Numbers & Factorial with Flowcharts
Harini
Harini

Posted on

LCM, Sum of N Numbers & Factorial with Flowcharts

What is LCM?

The Least Common Multiple (LCM) of two numbers is the smallest number that is divisible by both numbers.

Example:
LCM of 3 and 10 = 30
Because 30 is the smallest number divisible by both 3 and 10.

Flowchart

Python Code

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 is", big) 
        break 
    big += big2
Enter fullscreen mode Exit fullscreen mode

JavaScript Code

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 is " + big);
        break;
    }
    big += big2;
}
Enter fullscreen mode Exit fullscreen mode

Java Code

public class LCM {
    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 is " + big);
                break;
            }
            big += big2;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Sum of First N Numbers Using Flowchart (Python, Java, JavaScript)

Flowchart

Python Code

bag = 0
date = 1

while date <= 10:
    bag = bag + date
    date += 1

print("Sum is", bag)
Enter fullscreen mode Exit fullscreen mode

JavaScript Code

let bag = 0;
let date = 1;

while (date <= 10) {
    bag = bag + date;
    date++;
}

console.log("Sum is " + bag);
Enter fullscreen mode Exit fullscreen mode

Java Code

public class SumN {
    public static void main(String[] args) {
        int bag = 0;
        int date = 1;

        while (date <= 10) {
            bag = bag + date;
            date++;
        }

        System.out.println("Sum is " + bag);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Factorial of a Number Using While Loop (Python, Java, JavaScript)

The factorial of a number is one of the most common beginner-level programming problems.

Factorial of a number (n) is:

n! = n × (n-1) × (n-2) × ... × 1

Example:

10! = 10 × 9 × 8 × ... × 1 = 3628800

Flowchart

Python Code

fact = 1
no = 1

while no <= 10:
    fact = fact * no
    no += 1

print("Factorial is", fact)
Enter fullscreen mode Exit fullscreen mode

JavaScript Code

let fact = 1;
let no = 1;

while (no <= 10) {
    fact = fact * no;
    no++;
}

console.log("Factorial is " + fact);
Enter fullscreen mode Exit fullscreen mode

Java Code

public class Factorial {
    public static void main(String[] args) {
        int fact = 1;
        int no = 1;

        while (no <= 10) {
            fact = fact * no;
            no++;
        }

        System.out.println("Factorial is " + fact);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)