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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
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<x)
    {
        if (x%i==0)
        {
           x=x/i;
           y=y+i;


        }
        else
        {i++;}
     }


    int z;
    z=x+y;
    cout<<z<<endl;



}
}
let me edit this, so it's easier to read.
that's your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

int main()
{
    int i=2;
    int y=0;
    
	for (int k=2; k<1000; k++)
	{
		int x=k;
		
		while(i<x)
		{
			if (x%i == 0)
			{
				x = x/i;
				y = y+i;
			}
			else
				{i++;}
		}

		int z;
		z = x+y;
		std::cout << z << std::endl;
	}
}


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
1
2
3
4
5
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)
tamimaddari wrote:
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)
tamimaddari wrote:
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.

tamimaddari wrote:
I am a beginner?

I don't know. Are you?
yes I am, in fact thats why i didnt understand a word u asked to do. I did prog>file.txt though in cmd. and it just gives a blue screen to open files!
closed account (3TXyhbRD)
Is prog your executable name? (the .exe file)
Topic archived. No new replies allowed.