Question

In Scala How would I turn an integer like 509 into a list like (5,0,9)

In Scala

How would I turn an integer like 509 into a list like (5,0,9)

Homework Answers

Answer #1

/****************
* Scala program**
******************/

object Main extends App{
val numberInt = 509
println("The number is :"+ numberInt)
var digitList =List[Int]() //create an empty list of int
var x = numberInt
while( x > 0){
var digit = x % 10 //get the digit at unit's place
digitList::=digit //add the digit into list
x = (x / 10).toInt //divide x by 10 to convert n digit x to (n-1) digit
}
  
println(digitList)//print the digitlist
}

=======================

output

=======================

The number is :509
List(5, 0, 9)
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How would I make a linked list of a linked list in C? For example I...
How would I make a linked list of a linked list in C? For example I want a linked list that holds all the words inputted from the user. Example: typedef struct node{ char *word; struct node *next; }node; This list will hold all the strings that were inputted from the user. What I want to do is have another linked list that holds all the words that are no vowel matches which means that they would be equal if...
In MIPS, how would you add and multiply two integer numbers like 5 & 6 and...
In MIPS, how would you add and multiply two integer numbers like 5 & 6 and display on the screen?
I would like to know how to create an AR Aging table in IDEA?
I would like to know how to create an AR Aging table in IDEA?
Write a program that reads an integer, a list of words, and a character. The integer...
Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle z then...
I would like to add n bits all together, how many XOR, AND and OR gates...
I would like to add n bits all together, how many XOR, AND and OR gates I need at the minimum? Could you please explain your answer with examples like n=3,4,5 etc.
how foreign trade and policies can affect our real GDP? I would like to see how...
how foreign trade and policies can affect our real GDP? I would like to see how tariffs and currency falls into this
I would like to know how to record the asset retirement obligation on a new plant....
I would like to know how to record the asset retirement obligation on a new plant. I'm not sure of what all the information is needed to figure this out.
hat Artificial Intelligence is and how it is being used in healthcare. Here I would like...
hat Artificial Intelligence is and how it is being used in healthcare. Here I would like you to post what you think the biggest obstacle is to incorporating AI in healthcare.
how do i turn this acceleration in polar coordinates: a = 0.331er + 0.48e0 into cartesian...
how do i turn this acceleration in polar coordinates: a = 0.331er + 0.48e0 into cartesian form
How would I do a question like this: Create the probability distribution table (correct to 4...
How would I do a question like this: Create the probability distribution table (correct to 4 decimal places) for a binomial distribution with n=3 and p=0.4. I also have to show the full calculation for each row.