A simple C++ problem

Hi, I have written a program, that would find the prime factors of all numbers upto 1000. there should be 999 outputs. but the outs arent really the sum, what should i do? my code is.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include using namespace std; int main() { int i=2; int y=0; for (int k=2;k<1000; k++) { int i=2; int y=0; int x=k; while(i
let me edit this, so it's easier to read.
 ``123456789101112131415161718192021222324252627`` ``````#include int main() { int i=2; int y=0; for (int k=2; k<1000; k++) { int x=k; while(i

there are no 999 outputs, since your for loop runs from 2 to 999 --> 998outputs

line 16, you are dividing integers, no idea if you know what that means.
for example 10/3 = 3 and not 3.33333...
Last edited on
Do you mean list the prime factors of each number like this:
 ``` 2: 2 3: 3 4: 2 5: 5 6: 2 3 ```

if so, then the easiest thing to do would be to calculate all primes <1000 and the loop through all integers between 2 and 1000, and check to see if divisible by all of the primes under 1000.
if i give cin than it works,, in line 16 ,it will never be 10/3 cause i have set the condition if,x%i==0;
10/3 was just an example, thought the words "for example" made that clear
no script coder, i mean
 ``12345`` ``````2:2 3:3 4:2+2=4 5:5 6:2+3=5``````

and dark master, I think i am having the problem in the for loop;
closed account (3TXyhbRD)
Shouldn't i be set to 2 and y to 0 before the while loop?

Offtopic: Your code is non intuitive. What is the purpose of i, y, x, k and z? Use suggestive names to make it easier to understand how you're going at the problem.
Last edited on
I have found the problem. Its not in the sourcecode. Its actually in the display window of the program.when upto k<298 the program works fine. but when k=299 it omits the first result 2. when 300 than it omits 2 and 3, and go on!! Is there any way so that I can expand the display window of the command. I am using windows 7 and codeblocks....
@Script Coder: I would expect it to give repeating factors:
`4 = 2 * 2`

You do have a code error though, "i" needs to be reset to 2 for every factor that it finds.
closed account (3TXyhbRD)
I have found the problem. Its not in the sourcecode. Its actually in the display window of the program.when upto k<298 the program works fine. but when k=299 it omits the first result 2. when 300 than it omits 2 and 3, and go on!! Is there any way so that I can expand the display window of the command. I am using windows 7 and codeblocks....

Output to a file.

LowestOne wrote:
You do have a code error though, "i" needs to be reset to 2 for every factor that it finds.

And y to 0 right before the while loop.
Last edited on
How to output a file? I am a beginner?
closed account (3TXyhbRD)
How to output a file?

Either use ofstream or redirect from cmd (Windows)/terminal (Unix).

For cmd:
prog > file.txt

For terminal:
./prog > file.txt

Assuming prog is your executable name, all output using cout will be printed in file.txt instead of the screen.
Obviously you will need to navigate to the directory where your executable is (using cd and dir (Windows)/ls (Unix)).
For Windows type cd /? to see what it does. man ls for Unix.