ok, i've got a question that i'm having trouble with

Write a program that generates the factors of a number entered by the user. This program has the following requirements:
1. The user must enter a positive integer. If the user enters something else, your program should output an error message and let the user enter a new value. Use a do/while loop to make sure the user input is successful.
2. The factors should be output in increasing order. The lowest factor your program should report is 2. Your program should output 4 factors per line, each factor is in a field of 10 characters.
3. You will need a while loop to report the factors. Here are some helpful hints:
a. If (a % b == 0) then a is a factor of b.
b. When you have found a factor, output the factor and then reduce the number you are working with by dividing the number by the factor… ie) b = b / a;

my problem is, i can get the program to factor out, but i dont know how to get it to output in increasing order, or with 4 factors per line, with each factor in field of 10, here is part of my code

if (factor%2 == 0)
	{
		cout << 2 << " ";
		while (factor%2 == 0)
		{
			cout << factor << " ";
			factor = factor/2;
		}
	}

i'm not asking for something to copy and paste, just some guidance

Recommended Answers

All 5 Replies

Ok, i just realized what an idiot i am, i got the factor thing figured out, and putting in increasing order, but........can someone help me with the 4 factors per line, and each factor being in a field of 10...

while(number > 1)
	{
		
		if (number%factor == 0)
		{
			cout << factor << " ";
			number /= factor;
		}
		else 
		{
			factor++;
		}
	}

Ok, i just realized what an idiot i am, i got the factor thing figured out, and putting in increasing order, but........can someone help me with the 4 factors per line, and each factor being in a field of 10...

while(number > 1)
	{
		
		if (number%factor == 0)
		{
			cout << factor << " ";
			number /= factor;
		}
		else 
		{
			factor++;
		}
	}

You should post the whole program. It's hard to tell what's going on with just this fragment.

iomanip may help: http://www.cprogramming.com/tutorial/iomanip.html

If (a % b == 0) then a is a factor of b.

This is backwards. b is a factor or a, not vice versa.

Hmmm it seems your using a while loop to check if the input was valid. I would just throw an exception:

if (number <= 0)
     throw exception ("Invalid number");
//now do the factor testing loop
for (int factor(2); factor <= (number / 2); ++factor)
     if (number%factor == 0)
            	cout << factor << " ";
     else 
               factor++;

Ok, i just realized what an idiot i am, i got the factor thing figured out, and putting in increasing order, but........can someone help me with the 4 factors per line...

Count each value you output. When you get to 4, output an endl and reset your counter.

... and each factor being in a field of 10...

This has already been answered by VernonDozier.

Hmmm it seems your using a while loop to check if the input was valid. I would just throw an exception:

I might too if I were a 3-year veteran in C++. But a beginner probably has no idea what you're talking about. :icon_wink:

Errr oops. The code I posted won't work. Get rid of the else, since the factor doesn't need to be += 2. And also get rid of the / 2 in the for loop. I was thinking prime number not factor.

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.