[Ruby] How to get the tens place and the ones place

Overview

We will get the tens and ones digits from a two-digit integer and write until the calculation result is output.

table of contents

--Practice --Problem --Answer (commentary) --Another solution (digits method) --Another solution (divmod method)

--Summary

--References

Practice

problem

Write a program that adds ** addition and multiplication results ** of ** 10's and 1's ** numbers from a two-digit integer and outputs it.

Answer (commentary)

answer


def addition(a, b)
  a + b
end

#Multiplication of tens and ones
def multiplication(a, b)
  a * b
end

def check_num(num)
  tens_place = (num / 10) % 10   #The remainder is calculated after dividing the tens digit by 10 to make it one digit.
  ones_place = num % 10          #The remainder when divided by 10 is the 1st place

  addition_result       = addition(tens_place, ones_place)        #Add with the addition method
  multiplication_result = multiplication(tens_place, ones_place)  #Multiply with multiplication method

  p "The sum of the result of addition and the result of multiplication is#{ addition_result +  multiplication_result }is."
end

#Enter in the terminal
p 'Please enter a two digit integer'
num = gets.to_i

#Method call
check_num(num)

Another solution (digits method)

The rewritten part is where ** 10's place and 1's place are calculated **. The reason for specifying 2 in the argument of digits.take is that we are getting two values, the ones digit and the tens digit. Note that when you get it with digits.take, you get it in order from 1's place.

digits


def addition(a, b)
  a + b
end

def multiplication(a, b)
  a * b
end

def check_num(num)
  ones_place, tens_place = num.digits.take(2) #Get 1st and 10th place

  addition_result       = addition(tens_place, ones_place)       
  multiplication_result = multiplication(tens_place, ones_place) 

  p "The sum of the result of addition and the result of multiplication is#{ addition_result +  multiplication_result }is."
end

p 'Please enter a two digit integer'
num = gets.to_i

check_num(num)

Another solution (divmod method)

The rewritten part is where ** 10's place and 1's place are calculated **. By specifying a numerical value in the argument of divmod ( 10 in this case), you can get the quotient and remainder when divided.

divmod


def addition(a, b)
  a + b
end

def multiplication(a, b)
  a * b
end

def check_num(num)
  tens_place, ones_place = num.divmod(10) #Get 1st and 10th place

  addition_result       = addition(tens_place, ones_place)        
  multiplication_result = multiplication(tens_place, ones_place) 

  p "The sum of the result of addition and the result of multiplication is#{ addition_result +  multiplication_result }is."
end

p 'Please enter a two digit integer'
num = gets.to_i

check_num(num)

Summary

――The tens place is the remainder of dividing a two-digit integer by 10 and then dividing by 10. ――The ones digit is the remainder of dividing a two-digit integer by 10. --By using the digits method, you can get the numbers in order from the 1st place.

References

-Ruby 2.7.0 Reference Manual (digits) -Ruby 2.7.0 Reference Manual (divmod) -Get tens and ones digits from two digits -Find the quotient and remainder of division at once

Recommended Posts

[Ruby] How to get the tens place and the ones place
How to find the tens and ones
How to get date data in Ruby
[Rails] How to get the URL of the transition source and redirect
[Swift5] How to get an array and the complement of arrays
[Java] How to get the current directory
How to place and share SwiftLint config files on the server
How to get and add data from Firebase Firestore in Ruby
[Ruby] How to get the value by specifying the key. Differences between hashes, symbols and fetch
[Easy] How to upgrade Ruby and bundler
[For beginners] How to get the Ruby delayed railway line name
[Java] How to convert from String to Path type and get the path
[Java] How to get the current date and time and specify the display format
How to create your own annotation in Java and get the value
How to find the cause of the Ruby error
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Java] How to get and output standard input
[Ruby] How to use gsub method and sub method
How to get and study java SE8 Gold
How to find the total score and average score
To get Ruby Silver at the last minute
[Rails] How to get success and error messages
How to build the simplest blockchain in Ruby
[Java] How to get the authority of the folder
Ruby How to convert between uppercase and lowercase
I want to get the value in Ruby
[Ruby basics] How to use the slice method
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
[Ruby] How to calculate the total amount using the initialize method and class variables
[Java] How to get the URL of the transition source
Rails / Ruby: How to get HTML text for Mail
[Ruby] How to find the sum of each digit
[Ruby on Rails] How to change the column name
[Ruby] Method to easily get the receiver type .class
[Java] How to get the maximum value of HashMap
[Android] How to get the setting language of the terminal
How to handle TSV files and CSV files in Ruby
[Rails] How to get the contents of strong parameters
[Swift] How to get the document ID of Firebase
(Ruby on Rails6) How to create models and tables
[Kotlin] How to get IP address and user agent
[Swift5] How to round off, round down, and round up to the second or third decimal place
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
How to use Ruby return
[Ruby] How to comment out
Ruby: How to use cookies
[Ruby] How to write blocks
How to get the class name / method name running in Java
How to use Maven to place resource files outside the JAR
Difference between Java and JavaScript (how to find the average)
[IOS] How to get the table name from AWS DynamoDB
How to retrieve the hash value in an array in Ruby
How to get the longest information from Twitter as of 12/12/2016
How to get the log when install4j does not start
How to run React and Rails on the same server
How to check the extension and size of uploaded files
[jsoup] How to get the full amount of a document
How to batch run JUnit and get coverage as well
How to deal with different versions of rbenv and Ruby
[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition
[Ruby] Difference between get and post