DEV Community

Vidya
Vidya

Posted on

LCM,Factorial,Sum of Numbers Program in three languages

LCM(Least Common Multiple)
The Least Common Multiple (LCM) of two or more numbers is the smallest number that is exactly divisible by all the given numbers.

Example:

  Find LCM of 4 and 6:
      Multiples of 4 → 4, 8, 12, 16, 20...
      Multiples of 6 → 6, 12, 18, 24...
    --> The smallest common multiple is 12
    --> So, LCM(4, 6) = 12
Enter fullscreen mode Exit fullscreen mode

Flowchart

Java

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

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

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

output

Sum of Numbers
The sum of numbers means the result obtained when two or more numbers are added together.

Example:

        2 + 3 = 5 → Here, 5 is the sum
        10 + 20 + 30 = 60 → Here, 60 is the sum
Enter fullscreen mode Exit fullscreen mode

Flowchart

java

public class Sum {

    public static void main(String[] args) {
         int sum=0;
         int no=1;
         while(no<=10) {
             sum=sum+no;
             no++;
         }
          System.out.println("Sum is " + sum);
    }

}
Enter fullscreen mode Exit fullscreen mode

python

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

javascript

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

output

Factorial
The factorial of a number is the product of all positive integers from 1 up to that number.

Example:

      5! = 5 × 4 × 3 × 2 × 1 = 120
      4! = 4 × 3 × 2 × 1 = 24
Enter fullscreen mode Exit fullscreen mode

Flowchart

Java

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

python

                 fact=1;
         no=1;
         while no<=10):
             fact=fact*no
             no+=1
         print("Factorial is " , fact)

Enter fullscreen mode Exit fullscreen mode

javascript

                 let fact=1;
         let no=1;
         while(no<=10) {
             fact=fact*no;
             no++;
         }
                 console.log("Factorial is " + fact);

Enter fullscreen mode Exit fullscreen mode

output

Top comments (0)