1. Conclusion </ b>
2. How to use </ b>
3. What I learned from here </ b>
Remaining </ b>:% Exponentiation </ b>: ** use!
This time, a 2-digit integer (the program does not limit the number of digits) input "The sum of the tens digit and the ones digit is calculated.
python
def addition(a, b)
a + b
end
def calculation(num)
#10th place
no1 = (num / 10) % 10
#1st place
no2 = (num / 1) % 10
return no1,no2
end
puts "Enter a 2-digit integer"
num = gets.to_i
a, b = calculation(num)
add_sum = addition(a, b)
puts "The sum of the tens place and the ones place is#{add_sum}"
In order to get the tens place, the number divided by 10 is further divided by 10 to get the remainder. Similarly, the 1st place is the remainder obtained by dividing the value by 1 and further dividing by 10. At that time, I use "%" to put out too much.
(Example)For 28
(num / 10)・ ・ ・ ・ 28/ 10 = 2.8
2.8 %10 ・ ・ ・ ・ 0 remainder 2"2"return it.
(num / 1)・ ・ ・ ・ 28/ 1 = 28
28 %10 ... 2 remainder 8"8"return it.
I learned that a program with a mixture of arithmetic elements requires not only knowledge of the program but also the idea of devising calculations. Knowledge of the program is also required for devised calculations, and it is very important because it may be used pinpointly when wondering "where should this method be used?".
Recommended Posts