[RUBY] A program that determines whether the entered integer is close to an integer

Today we will solve this problem.

Question 1 Enter a positive integer. That integer is True if the difference from a multiple of 10 (10,20,30 ...) is within 2. Other than that, False Let's create a method that outputs.

Output example: near_ten(12)→True near_ten(17)→False near_ten(19)→True

Model answer

def near_ten(num)
  quotient = num % 10
  if quotient <= 2 || quotient >= 8
    puts "True"
  else
    puts "False"
  end
end

Commentary To think about "differences from multiples of 10", focus on the ones digit. In other words, if the ones place is any of "0,1,2,8,9", it can be judged that "the difference from the multiple of 10 is within 2".

Therefore, assign the ones digit value to the variable quote and quote<= 2 || quotient >=8 is "0",1,2,8,We are checking if any of the 9's are true.

Recommended Posts

A program that determines whether the entered integer is close to an integer
A memo of the program that allows you to realize that the probability of dice rolling is about 1/6
[Java] List method that determines whether a specific object is included
How to identify the path that is easy to make a mistake
[Ruby] A program that determines prime numbers
A program that calculates factorials from 2 to 100
I tried to make a program that searches for the target class from the process that is overloaded with Java
Processing when an ID that does not exist in the database is entered in the URL
A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...
Program to determine if it is a leap year
Null value is entered when assigning to an array
A program (Java) that outputs the sum of odd and even numbers in an array
A story that I realized that I had to study as an engineer in the first place
[Docker] Is it good enough to call it a multi-stage build? → The story that became so good
Determine that the value is a multiple of 〇 in Ruby
A program that searches for a character string, and when the search character string is found, displays the character string from the beginning of the line to just before the search character string.
A program that counts the number of words in a List
[ruby] Creating a program that responds only to specific conditions
Convert an array that may be null to a stream
The story of making a binding for libui, a GUI library for Ruby that is easy to install
About the method to convert a character string to an integer / decimal number (cast data) in Java
How to build an environment with Docker, which is the minimum required to start a Rails application
I checked with Ruby whether there is a Kaprekar number in an integer within a certain range
How to deal with the type that I thought about writing a Java program for 2 years
To write a user-oriented program (1)
A guide to "Something is slowing down ..." at the TCP layer
Mechanism for converting to a language that the browser can recognize
How to output the value when there is an array in the array
How to deploy an app that references a local jar to heroku
What is the difference between a web server and an application server?
How to store the information entered in textarea in a variable in the method
A good way to make a recursive function that reverses the characters
Let's write a code that is easy to maintain (Part 2) Name
When you receive a call, send an SMS to that number
Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
Vivaldi on ubuntu An issue where a transparent window that cannot be erased is created on the Desktop
About the matter that tends to be confused with ARG of Dockerfile which is a multi-stage build
The story of introducing Gradle as a retrofit to an existing system that did not manage packages