Advertisements
Advertisements
Question
Write program to print all common multiples of two numbers up to the product of the numbers.
Code Writing
Advertisements
Solution
import java.util.*;
class clCommon
{
void main()
{
Scanner sc = new Scanner(System.in);
int n1, n2;
System.out.print("Enter number1 : ");
n1 = sc.nextInt();
System.out.print("Enter number2 : ");
n2 = sc.nextInt();
System.out.println("Common Multiples of " +n1 + " & " + n2 + "are : ");
for(int m = Math.max(n1, n2); m<= n1 * n2; m++)
{
if(m%n1=0 && m%n2=0)
{
System.out.print("" + m);
}
}
}
}
Output:
Enter number 1 : 9
Enter number 2 : 15
Common Multiples of 9 & 15 are :
45 90 135
shaalaa.com
Is there an error in this question or solution?
