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 would I make a generic insertion sort for a doubly linked list in java? or...
How would I make a generic insertion sort for a doubly linked list in java? or is it even possible to make it generic in the first place?
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 implement the extended euclidean algorithm in Java which takes...
I would like to know how to implement the extended euclidean algorithm in Java which takes 2 integers, public int xgcd(int a, int b) and returns the multiplicative inverse of 'a' modulo 'b' and if the multiplicative inverse of 'a' does not exist it should return -1. I know to do it in a recursive manner but I need help with the non-recursive or iterative manner without using biginteger.math. Something like using the pseudocode would do. I would really appreciate...
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.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT