Q:

Kotlin program to check if given number is perfect square

belongs to collection: Kotlin Basic Programs

0

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

All Answers

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

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

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 find factorial of a number... >>
<< Kotlin program to find factors of a number...