1
   

How to do a program that asks the user positive integer and then prints its factors in in descending

 
 
Reply Wed 7 Mar, 2012 02:47 am
How to do a program that asks the user positive integer and then prints its factors in in descending order? If number is 120, then its factors are 5, 3, 2, 2, 2.
Some help please, thank you.
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 1 • Views: 2,246 • Replies: 3
No top replies

 
George
 
  2  
Reply Wed 7 Mar, 2012 11:19 am
@alvoutila,
To begin with, the factors of 120 are
2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40 and 60.

I'd suggest using a loop, initializing the index to 2 and stopping when the index
is equal to the number input.
Within the loop first divide input by the index and determine whether there
is no remainder (use modulo).
If there's no remainder, then save the index and the quotient in a list.
Also save the higher of the two numbers, replacing the previous higher
number.
As you continue to loop, you can break out of the loop once the index is equal
to the saved higher number.
At the end, sort the list in descending order and display it.
Kathleenortan
 
  0  
Reply Fri 1 Nov, 2013 03:30 pm
@George,
See the algorithm is first find the factors of the number that can be done as
k=0;
for(i=1;i<= number ;i++)
{
if(number %i==0)
{
a[k]=number;
k++;
}
}
now the factors are saved in the array a[], now apply bubble sort to this array you will get the array sorted in ascending order now print the array in opposite way
for(j=k;j>=0;j--)
{
print (a[j]);
}
you will get the factors in opposite order.
0 Replies
 
maxdancona
 
  1  
Reply Fri 1 Nov, 2013 03:48 pm
@George,
I think the example gave prime factors. I suspect that might be the assignment.
0 Replies
 
 

Related Topics

Clone of Micosoft Office - Question by Advocate
Do You Turn Off Your Computer at Night? - Discussion by Phoenix32890
The "Death" of the Computer Mouse - Discussion by Phoenix32890
Windows 10... - Discussion by Region Philbis
Surface Pro 3: What do you think? - Question by neologist
Windows 8 tips thread - Discussion by Wilso
GOOGLE CHROME - Question by Setanta
.Net and Firefox... - Discussion by gungasnake
Hacking a computer and remote access - Discussion by trying2learn
 
  1. Forums
  2. » How to do a program that asks the user positive integer and then prints its factors in in descending
Copyright © 2020 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.05 seconds on 08/09/2020 at 06:30:29