ADVERTISEMENT
ADVERTISEMENT

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

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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.