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
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.
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
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
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
References:
https://www.javatpoint.com/lcm-of-two-numbers-in-java
https://www.javatpoint.com/java-program-to-find-gcd-of-two-numbers
Top comments (0)