0

Working with numbers between 2 variables given in input

how do i work with numbers given in input? (example: given n1 and n2 in input, find all the multiple of 3 between n1 and n2)

13th Nov 2016, 4:04 PM
Luca De Guidi
Luca De Guidi - avatar
2 Respostas
+ 2
You can do a loop that starts in n1 (or n1 + 1 If you want to use just the numbers between) and ends in n2 (or n2 - 1, for the same reason that i said before).
13th Nov 2016, 4:48 PM
Lawrence Silva
Lawrence Silva - avatar
+ 2
Using your example: create a counter to add when you find a multiple of 3, then make this loop: for(int i = n1 + 1; i < n2; i++) { if(i % 3 == 0) { counter++; //equivalent to counter = counter+1 } }
13th Nov 2016, 4:51 PM
Lawrence Silva
Lawrence Silva - avatar