Find the nearest next perfect square of a number

Here given code implementation process.

// Java Program for
// Find the nearest next perfect square of a number
public class PerfectSquare
{
	public void nextPerfectSquare(int num)
	{
		// Calculate square root of given number
		int nextSqrt = (int) Math.floor(Math.sqrt(num));
		// Next perfect sqrt
		nextSqrt = nextSqrt + 1;
		// Display given number
		System.out.print("\n Given number : " + num);
		// Display next perfect sqrt
		System.out.print("\n Next Perfect Square (" + nextSqrt + "²) : " 
                         + (nextSqrt * nextSqrt));
	}
	public static void main(String[] args)
	{
		PerfectSquare task = new PerfectSquare();
		// Test Cases
		task.nextPerfectSquare(10);
		task.nextPerfectSquare(121);
		task.nextPerfectSquare(4);
	}
}

input

 Given number : 10
 Next Perfect Square (4²) : 16
 Given number : 121
 Next Perfect Square (12²) : 144
 Given number : 4
 Next Perfect Square (3²) : 9
// Include header file
#include <iostream>

#include <math.h>

using namespace std;
// C++ Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
	public: void nextPerfectSquare(int num)
	{
		// Calculate square root of given number
		int nextSqrt = (int) floor(sqrt(num));
		// Next perfect sqrt
		nextSqrt = nextSqrt + 1;
		// Display given number
		cout << "\n Given number : " << num;
		// Display next perfect sqrt
		cout << "\n Next Perfect Square (" << nextSqrt << "²) : " << (nextSqrt *nextSqrt);
	}
};
int main()
{
	PerfectSquare *task = new PerfectSquare();
	// Test Cases
	task->nextPerfectSquare(10);
	task->nextPerfectSquare(121);
	task->nextPerfectSquare(4);
	return 0;
}

input

 Given number : 10
 Next Perfect Square (4²) : 16
 Given number : 121
 Next Perfect Square (12²) : 144
 Given number : 4
 Next Perfect Square (3²) : 9
// Include namespace system
using System;
// Csharp Program for
// Find the nearest next perfect square of a number
public class PerfectSquare
{
	public void nextPerfectSquare(int num)
	{
		// Calculate square root of given number
		int nextSqrt = (int) Math.Floor(Math.Sqrt(num));
		// Next perfect sqrt
		nextSqrt = nextSqrt + 1;
		// Display given number
		Console.Write("\n Given number : " + num);
		// Display next perfect sqrt
		Console.Write("\n Next Perfect Square (" + nextSqrt + "²) : " + (nextSqrt * nextSqrt));
	}
	public static void Main(String[] args)
	{
		PerfectSquare task = new PerfectSquare();
		// Test Cases
		task.nextPerfectSquare(10);
		task.nextPerfectSquare(121);
		task.nextPerfectSquare(4);
	}
}

input

 Given number : 10
 Next Perfect Square (4²) : 16
 Given number : 121
 Next Perfect Square (12²) : 144
 Given number : 4
 Next Perfect Square (3²) : 9
<?php
// Php Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
	public	function nextPerfectSquare($num)
	{
		// Calculate square root of given number
		$nextSqrt = (int) floor(sqrt($num));
		// Next perfect sqrt
		$nextSqrt = $nextSqrt + 1;
		// Display given number
		echo("\n Given number : ".$num);
		// Display next perfect sqrt
		echo("\n Next Perfect Square (".$nextSqrt.
			"²) : ".($nextSqrt * $nextSqrt));
	}
}

function main()
{
	$task = new PerfectSquare();
	// Test Cases
	$task->nextPerfectSquare(10);
	$task->nextPerfectSquare(121);
	$task->nextPerfectSquare(4);
}
main();

input

 Given number : 10
 Next Perfect Square (4²) : 16
 Given number : 121
 Next Perfect Square (12²) : 144
 Given number : 4
 Next Perfect Square (3²) : 9
// Node JS Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
	nextPerfectSquare(num)
	{
		// Calculate square root of given number
		var nextSqrt =  Math.floor(Math.sqrt(num));
		// Next perfect sqrt
		nextSqrt = nextSqrt + 1;
		// Display given number
		process.stdout.write("\n Given number : " + num);
		// Display next perfect sqrt
		process.stdout.write("\n Next Perfect Square (" + nextSqrt + "²) : " 
                             + (nextSqrt * nextSqrt));
	}
}

function main()
{
	var task = new PerfectSquare();
	// Test Cases
	task.nextPerfectSquare(10);
	task.nextPerfectSquare(121);
	task.nextPerfectSquare(4);
}
main();

input

 Given number : 10
 Next Perfect Square (4²) : 16
 Given number : 121
 Next Perfect Square (12²) : 144
 Given number : 4
 Next Perfect Square (3²) : 9
import math
#  Python 3 Program for
#  Find the nearest next perfect square of a number
class PerfectSquare :
	def nextPerfectSquare(self, num) :
		#  Calculate square root of given number
		nextSqrt =  math.floor(math.sqrt(num))
		#  Next perfect sqrt
		nextSqrt = nextSqrt + 1
		#  Display given number
		print("\n Given number : ", num, end = "")
		#  Display next perfect sqrt
		print("\n Next Perfect Square (", nextSqrt ,"²) : ", 
              (nextSqrt * nextSqrt), end = "")
	

def main() :
	task = PerfectSquare()
	#  Test Cases
	task.nextPerfectSquare(10)
	task.nextPerfectSquare(121)
	task.nextPerfectSquare(4)

if __name__ == "__main__": main()

input

 Given number :  10
 Next Perfect Square ( 4 ²) :  16
 Given number :  121
 Next Perfect Square ( 12 ²) :  144
 Given number :  4
 Next Perfect Square ( 3 ²) :  9
#  Ruby Program for
#  Find the nearest next perfect square of a number
class PerfectSquare 
	def nextPerfectSquare(num) 
		#  Calculate square root of given number
		nextSqrt =  (Math.sqrt(num)).floor()
		#  Next perfect sqrt
		nextSqrt = nextSqrt + 1
		#  Display given number
		print("\n Given number : ", num)
		#  Display next perfect sqrt
		print("\n Next Perfect Square (", nextSqrt ,"²) : ", (nextSqrt * nextSqrt))
	end

end

def main() 
	task = PerfectSquare.new()
	#  Test Cases
	task.nextPerfectSquare(10)
	task.nextPerfectSquare(121)
	task.nextPerfectSquare(4)
end

main()

input

 Given number : 10
 Next Perfect Square (4²) : 16
 Given number : 121
 Next Perfect Square (12²) : 144
 Given number : 4
 Next Perfect Square (3²) : 9
// Scala Program for
// Find the nearest next perfect square of a number
class PerfectSquare()
{
	def nextPerfectSquare(num: Int): Unit = {
		// Calculate square root of given number
		var nextSqrt: Int =  Math.floor(scala.math.sqrt(num)).toInt;
		// Next perfect sqrt
		nextSqrt = nextSqrt + 1;
		// Display given number
		print("\n Given number : " + num);
		// Display next perfect sqrt
		print("\n Next Perfect Square (" + nextSqrt + "²) : " + 
              (nextSqrt * nextSqrt));
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PerfectSquare = new PerfectSquare();
		// Test Cases
		task.nextPerfectSquare(10);
		task.nextPerfectSquare(121);
		task.nextPerfectSquare(4);
	}
}

input

 Given number : 10
 Next Perfect Square (4²) : 16
 Given number : 121
 Next Perfect Square (12²) : 144
 Given number : 4
 Next Perfect Square (3²) : 9
import Foundation;
// Swift 4 Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
	func nextPerfectSquare(_ num: Int)
	{
		// Calculate square root of given number
		var nextSqrt = Int(floor(sqrt(Double(num))));
		// Next perfect sqrt
		nextSqrt = nextSqrt + 1;
		// Display given number
		print("\n Given number : ", num, terminator: "");
		// Display next perfect sqrt
		print("\n Next Perfect Square (", nextSqrt ,"²) : ", 
              (nextSqrt * nextSqrt), terminator: "");
	}
}
func main()
{
	let task = PerfectSquare();
	// Test Cases
	task.nextPerfectSquare(10);
	task.nextPerfectSquare(121);
	task.nextPerfectSquare(4);
}
main();

input

 Given number :  10
 Next Perfect Square ( 4 ²) :  16
 Given number :  121
 Next Perfect Square ( 12 ²) :  144
 Given number :  4
 Next Perfect Square ( 3 ²) :  9
// Kotlin Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
	fun nextPerfectSquare(num: Int): Unit
	{
		// Calculate square root of given number
		var nextSqrt: Int = Math.floor(Math.sqrt(num.toDouble())).toInt();
		// Next perfect sqrt
		nextSqrt = nextSqrt + 1;
		// Display given number
		print("\n Given number : " + num);
		// Display next perfect sqrt
		print("\n Next Perfect Square (" + nextSqrt + "²) : " 
              + (nextSqrt * nextSqrt));
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PerfectSquare = PerfectSquare();
	// Test Cases
	task.nextPerfectSquare(10);
	task.nextPerfectSquare(121);
	task.nextPerfectSquare(4);
}

input

 Given number : 10
 Next Perfect Square (4²) : 16
 Given number : 121
 Next Perfect Square (12²) : 144
 Given number : 4
 Next Perfect Square (3²) : 9

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved