Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 848
Muhammad Magdi Avatar asked Jan 04 '23 19:01

Muhammad Magdi


1 Answers

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)
like image 179
Alexander Romanov Avatar answered Jan 06 '23 08:01

Alexander Romanov