Least common multiple: Difference between revisions

no edit summary
(→‎{{header|Java}}: adding documentation)
No edit summary
Line 119:
(defn lcm [a b] (/ (* a b) (gcd a b)))</lang>
 
=={{header|Java}}==
<lang java>
import java.util.Scanner;
 
public class LeastCommonMultiple
{
public static void main(String[] args)
{
Scanner aScanner = new Scanner(System.in);//allows user input
int m, n, leastCommonMultiple;
//prompts user for values to find the LCM for, then saves them to m and n
System.out.print("Enter the value of m:");
m = aScanner.nextInt();
System.out.print("Enter the value of n:");
n = aScanner.nextInt();
int multipleOfM = m, multipleOfN = n;
/* this section increases the value of multipleOfM until it is greater
/ than or equal to the multipleOfN, then does it again when the lesser
/ becomes the greater--if they aren't equal*/
if (m != n)
{
for ( ; multipleOfM != multipleOfN ; )
{
if (multipleOfM < multipleOfN)
{
for ( int i = 0; multipleOfM < multipleOfN; i++)
{
multipleOfM = m * i;
}
}
else
{
for (int i = 0; multipleOfN < multipleOfM; i++)
{
multipleOfN = n * i;
}
}
}
leastCommonMultiple = multipleOfN;//equals multpleOfM
}
else
{
leastCommonMultiple = m;
}
System.out.println("Least Common Multiple of " +
m + " and " + n + " is: " + leastCommonMultiple);
}
}
</lang>
 
=={{header|Common Lisp}}==
Line 232 ⟶ 180:
0 0
0 1</lang>
 
=={{header|Java}}==
<lang java>
import java.util.Scanner;
 
public class LeastCommonMultiple
{
public static void main(String[] args)
{
Scanner aScanner = new Scanner(System.in);//allows user input
int m, n, leastCommonMultiple;
//prompts user for values to find the LCM for, then saves them to m and n
System.out.print("Enter the value of m:");
m = aScanner.nextInt();
System.out.print("Enter the value of n:");
n = aScanner.nextInt();
int multipleOfM = m, multipleOfN = n;
/* this section increases the value of multipleOfM until it is greater
/ than or equal to the multipleOfN, then does it again when the lesser
/ becomes the greater--if they aren't equal*/
if (m != n)
{
for ( ; multipleOfM != multipleOfN ; )
{
if (multipleOfM < multipleOfN)
{
for ( int i = 0; multipleOfM < multipleOfN; i++)
{
multipleOfM = m * i;
}
}
else
{
for (int i = 0; multipleOfN < multipleOfM; i++)
{
multipleOfN = n * i;
}
}
}
leastCommonMultiple = multipleOfN;//equals multpleOfM
}
else
{
leastCommonMultiple = m;
}
System.out.println("Least Common Multiple of " +
m + " and " + n + " is: " + leastCommonMultiple);
}
}
</lang>
 
 
=={{header|Prolog}}==
Anonymous user