Home »
Kotlin »
Kotlin Programs »
Kotlin Basic Programs
Kotlin program to check if given number is perfect square
Kotlin | Prefect Square: Here, we are implementing a Kotlin program to check if given number is perfect square.
Submitted by IncludeHelp, on May 12, 2020
Kotlin - Check if given number is perfect square
Given an integer number, we have to check whether it is perfect square or not.
Example:
Input:
num = 25
Output:
Perfect Square
Input:
num = 67
Output:
Not a Perfect Square
Program to check if given number is perfect square in Kotlin
package com.includehelp
import java.util.*
//import kotlin.math.floor
//import kotlin.math.sqrt
//function to check perfect Square number
fun checkPerfectSquare(number:Double):Boolean{
//Square Root of Number
val sq= Math.sqrt(number)
//Floor value, nearest Integer Part floor(983.2) = 983
val f = Math.floor(sq)
return sq==f
}
//Main Function , Entry point of Program
fun main(args: Array<String>) {
//Input Stream
val scanner = Scanner(System.`in`)
//Input Length of Rectangle
print("Enter Number : ")
val number = scanner.nextDouble()
//check Number and Print Result
if(checkPerfectSquare(number))
println("$number is Perfect Square")
else
println("$number is Not Perfect Square")
}
Output
Run 1:
Enter Number : 25
25.0 is Perfect Square
---
Run 2:
Enter Number : 67
67.0 is Not Perfect Square