Factors

I need help with prompting the user for an integer. Print out the factors of the integer in descending order.
A while loop can be used i suppose.
Please advise.
Thank you.
closed account (48T7M4Gy)
What do you need help or advise (sic) on?

The first step is to draft up some pseudocode or, with this problem, you might go straight to putting together some code.

There are many ways to solve this puzzle but a while loop or two might indeed be useful with this problem.
it would not matter how as long as it meets the criteria " Print out the factors of the integer in descending order."
I have this code, but it says that the value isn't declared?!! not sure why b/c i've declared it: int value;

#include <iostream>


using namespace std;
int main ()
{
int value;
while (int n =value != -1)
{
cin >> value;
}

return 0;

}
it needs to meet this when user enters a value:
Example:
Input:
25

Output:
25
5
1
closed account (48T7M4Gy)
The error with value is due to two things. When it is first encountered it's value is unknown. And then later on via the cin the user inputs it's value but it is too late.

Yes, you have declared value, the problem is you haven't initialised it.

where'd i initialize the value then?
So, i guess if i put the
cin >> value; first

int main()
int value;
cin >> value;
while (int n=value !=-1)

return 0;
Last edited on
closed account (48T7M4Gy)
Yep, put the cin first.

while (int n=value !=-1) is a problem.

Just write out in plain English what you are trying to do here. It is not clear at all and a computer will not have a clue either.

What is n? Is it a number, character, file name, ... ? What value does n have?

What you have written as it stands is something like " as long as some integer called n which I don't know anything about in terms of size, is equal to an integer I do know about, which is not equal to -1 then ... "

BTW don't forget equality in logical statements is ==, not =




I want to prompt the user for an integer. Print out the factors of the integer in descending order. Currently i have an infinite loop..

Example:
Input:
25

Output:
25
5
1

int value;

int main()
{
while (value !=-1)
{
cin >> value;

}

return 0;

}
1
2
3
4
5
6
7
8
int num = 0;

Ask user for a number -> store it in num

Loop through numbers starting at num, decrementing
        Use the modulo operator (%) to find the factors 
        if num % i is equal to 0, i is a factor
        Print i
Topic archived. No new replies allowed.