Kotlin program to generate 4 digits OTP

Kotlin | Generating 4-digits OTP: Here, we are going to learn how to generate a 4-digits OTP in Kotlin? Submitted by IncludeHelp, on April 16, 2020

Kotlin - Generate 4 digits OTP

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

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.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.