WAP to print the smallest positive number that is divisible by all the
numbers from M to N.
Answers
Explanation:
include <iostream>
#include <math.h>
using namespace std;
// Function to find the smallest
// N-digit number divisible by N
void smallestNumber(int N)
{
Answer:
The number 2520 is the smallest positive number that can be divided by each of the numbers from M to N without any remainder.
Explanation:
Below is the sample program solution:
public class Solution {
public ArrayList<Integer> list = new ArrayList<Integer>();
// creating a list of integers from 1 to 20 where M = 1 and N = 20
public ArrayList<Integer> addtolist() {
for (int i = 1; i <= 20; i++) {
list.add(i);
}
return list;
}
// finds the smallest positive number that is evenly divisible by all
of the numbers from 1 to 20
public int find() {
int num = 2520;
while(true) {
for(int i: list) {
if(num % i == 0) {
return num;
}
else {
num = num + 1;
}
}
}
}
public static void main(String[] args) {
Solution sol = new Solution();
sol.addtolist();
System.out.println(sol.find());//2520
}
}