0
0
RubyProgramBeginner · 2 min read

Ruby Program to Find Sum of Digits

You can find the sum of digits in Ruby by converting the number to a string, splitting it into characters, converting each back to integer, and summing them like this: number.to_s.chars.map(&:to_i).sum.
📋

Examples

Input123
Output6
Input0
Output0
Input99999
Output45
🧠

How to Think About It

To find the sum of digits, think of the number as a group of single digits. You separate each digit, change it to a number if needed, then add all these digits together to get the total sum.
📐

Algorithm

1
Get the input number.
2
Convert the number to a string to access each digit.
3
Split the string into individual characters.
4
Convert each character back to an integer.
5
Add all the integers together to get the sum.
6
Return or print the sum.
💻

Code

ruby
puts "Enter a number:"
number = gets.chomp
sum = number.chars.map(&:to_i).sum
puts "Sum of digits: #{sum}"
Output
Enter a number: 123 Sum of digits: 6
🔍

Dry Run

Let's trace the input '123' through the code

1

Input number

number = '123'

2

Split into characters

number.chars = ['1', '2', '3']

3

Convert to integers

['1', '2', '3'].map(&:to_i) = [1, 2, 3]

4

Sum digits

[1, 2, 3].sum = 6

5

Print result

Output: 'Sum of digits: 6'

IterationDigit (char)Digit (int)Running Sum
1111
2223
3336
💡

Why This Works

Step 1: Convert number to string

We convert the number to a string so we can look at each digit separately using to_s.

Step 2: Split string into characters

Splitting the string with chars gives us an array of digit characters.

Step 3: Convert characters to integers and sum

We convert each character back to an integer with map(&:to_i) and then add them all using sum.

🔄

Alternative Approaches

Using arithmetic operators
ruby
puts "Enter a number:"
number = gets.to_i
sum = 0
while number > 0
  sum += number % 10
  number /= 10
end
puts "Sum of digits: #{sum}"
This method uses math to extract digits without converting to string, which can be faster for large numbers.
Using recursion
ruby
def sum_digits(n)
  return 0 if n == 0
  n % 10 + sum_digits(n / 10)
end
puts "Enter a number:"
number = gets.to_i
puts "Sum of digits: #{sum_digits(number)}"
This recursive method breaks the problem into smaller parts but may be less efficient for very large numbers.

Complexity: O(n) time, O(n) space

Time Complexity

The program processes each digit once, so time grows linearly with the number of digits, making it O(n).

Space Complexity

Converting the number to a string and then to an array of digits uses extra space proportional to the number of digits, so O(n).

Which Approach is Fastest?

The arithmetic method uses constant space O(1) and is generally faster for very large numbers, while the string method is simpler and more readable.

ApproachTimeSpaceBest For
String conversion and sumO(n)O(n)Readability and simplicity
Arithmetic operatorsO(n)O(1)Performance with large numbers
RecursionO(n)O(n)Learning recursion, less efficient
💡
Use number.to_s.chars.map(&:to_i).sum for a clean and simple solution.
⚠️
Beginners often forget to convert characters back to integers before summing, causing errors or incorrect results.