hi can you help me please Write a program that breaks a number into its digits. Program keeps prompting the user to enter an integer and displays the digits of that integer until user enters -1

run like that

Enter a number (-1 to end): 3457 ↵
Digit1 = 7
Digit2 = 5
Digit3 = 4
Digit4 = 3
Enter a number: 23
Digit1 = 3
Digit2 = 2
Enter a number: 847570
Digit1 = 0
Digit2 = 7
Digit3 = 5
Digit4 = 7
Digit5 = 4
Digit6 = 8
Enter a number: -1

Recommended Answers

Yes, we can help. Show what you have done so far and explain what you need to progress further.

Jump to Post

All 2 Replies

Yes, we can help. Show what you have done so far and explain what you need to progress further.

Here is a working version. Unfortunately in the wrong language, looks like OP will have to do some work after all.

loop do

  puts "Enter a number (-1 to end):"
  num = gets.strip

  exit if num == "-1"

  num
    .chars
    .each
    .with_index(1) {|c,i| puts "Digit#{i} = #{c}"}

end
commented: Noice. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.