DEV Community

Sasireka
Sasireka

Posted on

Sum of numbers, Factorial, and LCM

1)Sum of first n numbers

  • Flowchart

  • Python
sum = 0
no = 1

while no <= 10:
    sum = sum + no
    no = no + 1

print("Sum =", sum)

Enter fullscreen mode Exit fullscreen mode

Output

  • Javascript
let sum = 0;
let no = 1;

while (no <= 10) {
    sum = sum + no;
    no = no + 1;
}

console.log("Sum =", sum);
Enter fullscreen mode Exit fullscreen mode
  • Java
public class SumOfNumbers {
    public static void main(String[] args) {
        int sum = 0;
        int no = 1;

        while (no <= 10) {
            sum = sum + no;
            no = no + 1;
        }

        System.out.println("Sum = " + sum);
    }
}
Enter fullscreen mode Exit fullscreen mode

2)Factorial of a number

  • Flowchart

  • Python
fact = 1
no = 1

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

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

Output

  • Javascript
let fact = 1;
let no = 1;

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

console.log("Factorial =", fact);
Enter fullscreen mode Exit fullscreen mode
  • Java
public class FactorialExample {
    public static void main(String[] args) {
        int fact = 1;
        int no = 1;

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

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

3)Least Common Multiple(LCM)

  • Flowchart

  • 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

Output

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

Top comments (0)