Q:

Kotlin program to generate 4 digits OTP

belongs to collection: Kotlin Basic Programs

0

OTP stands for "One Time Password" is a 4-8 digit alphanumeric code which is sent to the user via email or phone number for validation. As the name suggests, it can be used once only.

OTP's are majorly used in smartphone logins or signups that use phone-based validations. And, Kotlin is a programming language that might work with OTPs for validations. So, we should be familiar with the generation process of OTP using Kotlin programming language.

Example:

    OTP: 7997

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to generate 4 digits OTP in Kotlin

/**
    * Kotlin Program to Generate 4 digit OTP
*/
package com.includehelp.basic

/**
    * Function for Generate 4 digit OTP String
    * @return
*/
fun generateOTP(): String {
    val randomPin = (Math.random() * 9000).toInt() + 1000
    return randomPin.toString()
}

// Main Method Entry Point of Program
fun main(args: Array<String>) {
    // Call function for Generate OTP
    val otp1 = generateOTP()
    // Print OTP
    println("OTP : $otp1")
}

Output

Run 1:
OTP : 7997
-----
Run 2:
OTP : 7682
-----
Run 3:
OTP : 6934
-----
Run 4:
OTP : 4189

In this program, we have generated a 4-digits numerical OTP. using the similar process, you can generate OTP's of other lengths and type also.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Kotlin program to print Kotlin, JVM version (print... >>
<< Kotlin program to calculate the sum of natural num...