we did this problem today in math class to kill time after a quiz, and i was wondering if i could make a program along these lines
so the idea is this:
take the number 123,456,789
the first digit from the left is divisible by 1, good, continue
the first 2 digits from the left are divisible by 2, good, continue
the first 3 digits from the left are divisible by 3, good, continue
the first 4 digits from the left are NOT divisible by 4, you get a remainder, bad, restart with different numbers, or go back until one of the numbers can be replaced (the even spaces are pretty interchangeable)
the tricky part here is that you cant use the same integer twice
the only place we can be sure about is the fifth place, so we can tell that the number will look something like: _ _ _, _ 5 _, _ _ _
i want the program to print the number(s) which is(are) perfectly divisible, all the way to the ones place. im pretty sure that only one number fits this criteria, but id like to know if this is true, which is why im trying to make this program.
what i need help with is how i should go about checking if each number divided by its place has no remainder, and how to go back if it doesnt fit.
pretty new to coding..anything helps
You could setup a loop, that uses the modulus operator (%) to get the remainder of a number. I would setup an array with a legnth of 10, and modulus the number by 10 and put the result into the array (because you will read the numbers in reverse you need to place them into the array in reverse), until you have each digit.
I would then setup an int (named maybe
value), and put the first value in the array, into this int. I would also designate another int (named maybeplace) and set it equal to 1. Define a bool (named maybeflag).I would then enter a loop so that you have the equation
if(value % place == 0) flag = 0.Then have the loop break if
flag == 1, and goto a label before the loop, where you have an algorithm that generates a new number (maybe somthing as simple as adding one, or what ever you want). Also inside the loop ifplace == 10and your last value in the number is 0, you need to print the number, and then jump to your label from earlier, before the loop, to pick a new number. Also don’t forget that ifflag == 0, andplace != 10(the number hasn’t failed yet, but isn’t at the end yet) , that you need to multiply thevalueby 10 , then add the next number in the array to be able to test the next place. And you also need to increaseplaceby 1 each time.You said you’re new, so I’ll include a few definations: