Muhammad Magdi
Muhammad Magdi

Reputation: 150

Kotlin input as a BigInteger

I want to read two 50-digit numbers and print their sum, but I can't get input in Kotlin as BigInteger.

  1. How can I read Kotlin input as BigInteger?
  2. Are there any other way to solve such a problem?

Upvotes: 4

Views: 991

Answers (1)

Alex Romanov
Alex Romanov

Reputation: 11963

You can do it the same way as you would in Java:

val scanner = Scanner(System.`in`)
val first = scanner.nextBigInteger()
val second = scanner.nextBigInteger()

print(first + second)

OR you can use readLine() from kotlin.io:

val first = BigInteger(readLine())
val second = BigInteger(readLine())

print(first + second)

Upvotes: 9

Related Questions