If x is a factor of y then the remainder if y is divided by x is zero. Hence, the following is an algorithm for showing the factors of a number.
1. Read the number
2. Let candidate = 1
3. if number modulo candidate equals zero then print candidate (because candidate is a factor)
4. Let candidate be increased by 1
5. if candidate <= number then repeat from step 3
NOTES:
a. Recall that the modulo operator in java is symbolized b %. ( i.e. x%y returns the remainder when x s divided by y)
b. The algorithm can be implemented by a for loop, while loop or do-while loop
c. It is best if the program implementation follows the topdown approach( a.k.a. Stepwise refinement)
This comment has been removed by the author.
ReplyDelete