DEV Community

Kaviya.k
Kaviya.k

Posted on

GCD AND LCM:

Introduction to GCD and LCM:(TBD)

GCD (Greatest Common Divisor) and LCM (Least Common Multiple) are fundamental concepts in number theory and mathematics. They are used to find relationships between two or more numbers and play a significant role in various mathematical computations and problem-solving.

GCD:(TBD)

The GCD of two or more integers is the largest positive integer that divides each of the given numbers without leaving a remainder. In other words, it is the greatest common factor shared by the numbers. GCD is often denoted by the symbol "gcd(a, b)" or simply "(a, b)".
For example to :
The GCD of { 34,56 } is 2
The GCD of { 64,96 } is 32

How to Find GCD?:(TBD)
Write all the factors of each number.
Select the common factors.
Select the largest number, as GCD.

program:(TBD)

    public class FindGCDExample2  
    {  
    public static void main(String[] args)   
    {  
    int n1=50, n2=60;  
    while(n1!=n2)   
    {  
    if(n1>n2)  
    n1=n1-n2;  
    else  
    n2=n2-n1;  
    }  
    System.out.printf("GCD of n1 and n2 is: " +n2);  
    }  
    }  

Output:
GCD of n1 and n2 is: 10


Enter fullscreen mode Exit fullscreen mode

LCM:(TBD)

The LCM of two or more integers is the smallest positive integer that is divisible by each of the given numbers. It is the least common multiple shared by the numbers. LCM is often denoted by the symbol "lcm(a, b)" or simply "[a, b]". For example to :
The LCM of { 34,56 } is 952
The LCM of { 64,96 } is 192

How to Find the Greatest Common Factor:(TBD)

Write all the factors of each number.
Select the common factors.
Select the greatest number, as GCF.

program:(TBD)
    public class LcmExample1  
    {  
    public static void main(String args[])   
    {  
    int a = 12, b = 9, gcd = 1;  
    //finds GCD  
    for(int i = 1; i <= a && i <= b; ++i)   
    {  
    //divides both the numbers by i, if the remainder   is 0 the number is completely divisible by i  
    //Checks that i is present in both or not  
    //returns true if both conditions are true  
    if(a % i == 0 && b % i == 0)  
    //assigns i into gcd  
    gcd = i;  
    }  
    //determines lcm of the given number  
    int lcm = (a * b) / gcd;  
    //prints the result  
    System.out.printf("The LCM of %d and %d is %d.", a, b, lcm);  
    }  
    } 
output: 
  The LCM of 12 and 9 is 36.

Enter fullscreen mode Exit fullscreen mode

Task:

program 1:


package Numbers;

public class Pattern 
{
public static void main(String[] args) {
        int count=1;
        while (count<=5){
        System.out.println("1 2 3 4 5");
        count++;
        }
        }
                }
output:

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

program 2:

package Numbers;

public class Pattern2 {

    public static void main(String[] args) {
        int count=1;
        while (count<5){
        System.out.println("1 0 1 0 1 ");
        count++;
        }
                }
                }

output:
1 0 1 0 1 
1 0 1 0 1 
1 0 1 0 1 
1 0 1 0 1 
Enter fullscreen mode Exit fullscreen mode

program 3:

package Numbers;

public class Multiply {

    public static void main(String[] args) {
        int num=1;
        while(num<=10) {
      System.out.println("3*"+ num +"="+(3*num));
            num++;
        }
}
}

output:

3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30


Enter fullscreen mode Exit fullscreen mode

References:
https://www.javatpoint.com/lcm-of-two-numbers-in-java
https://www.javatpoint.com/java-program-to-find-gcd-of-two-numbers

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more