0
Given a number x = 8, find a number from 2 to 9, which can divide x without remainder
when dividing x by the number you find from 2 to 9 the quotient becomes x again. and you have to keep on dividing by a number form 2 to 9 until you get 1 as the quotient. when you get 1 as the quotient. find the product of all the number from 2 to 9. that you choose for dividing x, please i have been trying this for the last three days any help is welcome https://code.sololearn.com/cf7HI06mGNfb/?ref=app
1 Odpowiedź
+ 3
Going through the assignment, a solution could look like the following:
Declare two variables, the first holds the value you are supposed to divide, the other the product of our divisors, and initialize the result to 1.
Get 'x' from the user (or declare it 8, or whatever, basically).
Run a while loop that goes on as long as 'x' is not equal to 1.
Inside the while loop, use a for loop to go through the numbers from 2 to 9.
Inside the for loop, you then check if 'x' divided by the current number has no remainder, and if that is the case, divide 'x' by the current number while multiplying the result with the current number. Optionally, you could also display the divisor you just found to the screen, for the user to track the product.
After the while loop finishes running, all that is left is to display the result variable, since it equals the product.
You also might want to check for 'x' not being a negative number or 'x' being equal to 0 when acquiring it from the user.