Finding Factors of a number and more...
Author |
Message |
bigbut600
|
Posted: Wed Oct 08, 2008 8:22 pm Post subject: Finding Factors of a number and more... |
|
|
Hey guys, I am in need of help with a question om doing.
1) Im supposed to find and output factors of an integer between 1-50.
Im trying to do this with "if" but Im still new on it.
2) on the same output execution....
Im supposed to find all the factors of the number range between the integer chosen
so if the user typed in 6, for the integer between 1-50
i have to output all the factors of 6,
then output all the factors of the numbers from 1 - 6 (or watever number that was chosen to be the integer)
Im mostly trying to understand the 1st question.
Please any help wud be appreciated, if your willing to of course...
so far iv got...
code: |
var integer: int
var count: int := 0
var factor: real
put "Enter an integer between 1 and 50."
get integer
put "The factors of ", integer, " are:"
loop
count:= count + 1
factor:= (integer div count)
put factor
if (factor*count) = integer
then put factor
end if
exit when factor = 1
end loop
|
i got 2 problems when I execute the program.
1) something's up with the "div" Im not sure what
2) at my if statement, im saying that, if the factor i got above the line x count and = the integer
then put that factor
- but it is puting the factor down twice |
|
|
|
|
|
Sponsor Sponsor
|
|
|
The_Bean
|
Posted: Wed Oct 08, 2008 8:33 pm Post subject: Re: Finding Factors of a number and more... |
|
|
1) div divides and rounds at the same time
2) it's putting down twice because you have:
put factor
and since your if statement will always be true because
factor = integer div count %so
integer = factor * count
an the you put it again |
|
|
|
|
|
bigbut600
|
Posted: Wed Oct 08, 2008 8:42 pm Post subject: Re: Finding Factors of a number and more... |
|
|
The_Bean @ Wed Oct 08, 2008 8:33 pm wrote: 1) div divides and rounds at the same time
2) it's putting down twice because you have:
put factor
and since your if statement will always be true because
factor = integer div count %so
integer = factor * count
an the you put it again
im not so sure what you are saying for (2)
is it possible you can show me how not to get the factors to repeat itself?
i sorta learn better when i can visualize the work
O and I have 1 more problem that i need help with.
How can I make it so that only "whole numbers that divide into the integer evenly" will be outputted.
if i enter in 7 as the integer.
as my output i want to have
"The factors of 7 are:"
7
1
instead im getting
7
7
3
2
1 |
|
|
|
|
|
The_Bean
|
Posted: Wed Oct 08, 2008 8:53 pm Post subject: Re: Finding Factors of a number and more... |
|
|
Those aren't actually even factors.
Try putting in a prime number.
17: 17,17,8,5,4,3,2,2,2,1
where 17 and 1 are the only ones.
Try using 2 for statement from 1..integer instead of a loop
if the first for statement * the second for statement = integer then
put the first for statement
and new season of South Park is out NOW |
|
|
|
|
|
bigbut600
|
Posted: Wed Oct 08, 2008 9:35 pm Post subject: Re: Finding Factors of a number and more... |
|
|
The_Bean @ Wed Oct 08, 2008 8:53 pm wrote: Those aren't actually even factors.
Try putting in a prime number.
17: 17,17,8,5,4,3,2,2,2,1
where 17 and 1 are the only ones.
Try using 2 for statement from 1..integer instead of a loop
if the first for statement * the second for statement = integer then
put the first for statement
and new season of South Park is out NOW
Ermmm, I sorta did it....i guess, but 1 more problem left
I made a new posts bout it
Thanks for your help. |
|
|
|
|
|
|
|