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.
the output of the code is going forever, i just wanna the first output...help!!!!
It also has a hint:
1. "Use an outer loop to examine successive natural numbers"
2. "Use an inner loop will test the given number for divisibility by numbers from 2 to k"
The outer loop should thus start with some value of n and increase the n until it can be divided. k does not change during the algorithm.
For example, k==3.
n=1: not possible
n=2: not possible
n=3: cannot divide with 2
n=4: cannot divide with 3
n=5: cannot divide with 2
n=6: success, answer is 6
bool divisible( int n, int k )
{
// one do..while loop
}
This function should return true if n can be divided by all whole numbers in range [2..k]
Otherwise return false.
Can you write the implementation for this function?
bool divisible( int n, int k )
{
int k, n = 0;
bool done=true;
do{
// i have no idea in this, except the for loop..
}while(k > 1 && k <= 22 && !done);
}