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;
            unsigned int i = 2;
            if(n % i == 0)
                n += k;
                done = false;
                done = true;
        while(k % 2 == 1);
        cout << n;
    while(k > 1 && k <= 22 && !done);
    return 0;

Here is my input:

Here is my output:

Expected output:

Is there anything I need to add or fix? Any help would be appreciated.

Your solution looks too complicated for the task. I'm not going to bother trying to unravel it. Also, it looks like you didn't break down the problem well enough. Spend time and think about how you solve it before writting down code.

Also use comments. It looks like you got confused by your own logic, and ended up writting some unusual things.

I'm going to try to break this down for you.

1) You are given a k. You must validate k to make sure it's an integer more then 1 and less then 22. k shound NEVER be modified beyond this point. Think about it: If k changes, then your changing the question. (I can see when you wrote k++ in your old code, and at that point you didn't know what your program was doing).

2) The outerloop has one job. Iterate i starting from 2 and ending when the test passes. It's that simple. You don't need to validate k on. Every. Single. Iteration. Keep it simple, because this loop IS simple. If you want, you can exit the loop using a 'done condition' and that's it (there are more advanced ways to exiting out of nested loops, but I don't think it's worth learning them at this point).

3) The innerloop has one job. Iterate j starting from 2 and ending with k. Inside of this loop, i is tested against j. If it fails, then continue the outerloop. If it passes, then continue the innerloop. If the exit condition is met, then we have an answer.

And that's it. Just keep it simple and this question is easy.

Bonus: A MUCH faster way to calculate the answer would be to multiply all of the prime numbers under k togeather. This can be very quicky worked out using a seive.

commented: Great answer! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.