question

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!!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> 
using namespace std; 

int main() 
{ 
	int k, n = 0;
	cout<<"Enter a number: ";
	cin >> k; 
	do 
	{ 
		for (int i=2; i<=k; k++)
		{ 
			if(n % i == 0) 
			{ 
				n+=i;
			} 
		}
		cout << n << endl;
	}while(k > 1 && k <= 22); 
	return 0; 
} 
for (int i=2; i<=k; k++)

'i' is (presumably) your loop counter... but you do not increment it.

Instead, you are incrementing 'k'.
u r right, but i already know this. however, i dont know how to fix this.

this makes me headache..
Last edited on
If you want to keep looping until i reaches k, then i will have to change, not k.
how to change the "i"?
increment i instead of k

for (int i=2; i<=k; k++) // <- you are incrementing k
ohhhhhh.....thx
the output is still not right
bc of the for loop, n keep running and never stop
Your task has a restriction:
Your program may only use do-while loops

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
1
2
3
4
if(n % i == 0) 
{ 
	n+=i; //here is not right.....so....damn...so confuse, how to get the n from if(n % i == 0) 
}
i should to find k,
if k==6

n should % 2,3,4,5,6, if they are all equal to 0, we get it. we get the n.
in my opinion, k is the only input
The k is the only input.

Lets try a simpler thing. A function:
1
2
3
4
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
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); 
}
You already have n and k as parameters, so they should not be masked. Remove line 3.

However, you do need a number that gets values from 2 to k.

Assignment said that you can assume the k to be valid. Therefore, k is <=22.

There are no loops inside the loop.

What should each iteration of the loop do? Two things:
1. Can the number divide n?
2. Increment the number.

When should the loop stop? Two cases:
1. Number has successfully reached k
2. Number cannot divide n
Topic archived. No new replies allowed.