Write a complete program that reads an integer k > 1 from the console and finds the first positive natural number n hat is divisible by all whole numbers between 2 and k inclusive.
Your program may only use do-while loops , even if you think a for-loop or while loop is more appropriate. Hint: use an outer loop to examine successive natural numbers for the required property. Use an inner loop will test the given number for divisibility by numbers from 2 to k. You may assume that k is never bigger than 22. (Otherwise we may run into problems with integer overflow.) Your program should produce no other output than the integer n. Examples: If k is 2, n should be 2. If k is 7, n should be 420. If k is 17, n should be 12252240.
#include <iostream>
using namespace std;
int main()
{
int k;
int n = 0;
bool done = true;
cin >> k;
do
{
do
{
unsigned int i = 2;
if(n % i == 0)
{
n += k;
k++;
done = false;
}
else
{
done = true;
}
}
while(k % 2 == 1);
cout << n;
}
while(k > 1 && k <= 22 && !done);
return 0;
}
Here is my input:
2
Here is my output:
55
Expected output:
2
Is there anything I need to add or fix? Any help would be appreciated.