# Check if a number is perfect square

Here given code implementation process.

```
//C Program
//Check if a number is perfect square
#include <stdio.h>
void perfect_square(int number) {
//Use to check perfect square
int status = 0;
int i = 1;
for (i; i <= number / 2 && i *i <= number; ++i) {
if (i *i == number) {
//When number is perfect square
status = 1;
break;
}
}
if (status == 0) {
printf("%d Is not a perfect square \n", number);
} else {
printf("%d Is an perfect square of %d \n", number, i);
}
}
int main() {
//Test Case
perfect_square(16);
perfect_square(31);
perfect_square(81);
perfect_square(25);
return 0;
}
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

```
/*
C++ Program
Check if a number is perfect square
*/
#include<iostream>
using namespace std;
class MyNumber {
public:
void perfect_square(int number) {
//Use to check perfect square
int status = 0;
int i = 1;
for (i = 1; i <= number / 2 && i *i <= number; ++i) {
if (i *i == number) {
//When number is perfect square
status = 1;
break;
}
}
if (status == 0) {
cout << number << " Is not a perfect square \n";
} else {
cout << number << " Is an perfect square of " << i << "\n";
}
}
};
int main() {
MyNumber obj;
//Test case
obj.perfect_square(16);
obj.perfect_square(31);
obj.perfect_square(81);
obj.perfect_square(25);
return 0;
}
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

```
/*
Java Program
Check if a number is perfect square
*/
public class MyNumber {
void perfect_square(int number) {
//Use to check perfect square
int status = 0;
int i = 1;
for (i=1; i <= number / 2 && i *i <= number; ++i) {
if (i*i == number) {
//When number is perfect square
status = 1;
break;
}
}
if (status == 0) {
System.out.print(number+" Is not a perfect square \n");
} else {
System.out.print(number+" Is an perfect square of "+ i+"\n");
}
}
public static void main(String[] args) {
MyNumber obj = new MyNumber();
//Test case
obj.perfect_square(16);
obj.perfect_square(31);
obj.perfect_square(81);
obj.perfect_square(25);
}
}
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

```
/*
C# Program
Check if a number is perfect square
*/
using System;
public class MyNumber {
void perfect_square(int number) {
//Use to check perfect square
int status = 0;
int i = 1;
for (i = 1; i <= number / 2 && i * i <= number; ++i) {
if (i * i == number) {
//When number is perfect square
status = 1;
break;
}
}
if (status == 0) {
Console.Write(number + " Is not a perfect square \n");
} else {
Console.Write(number + " Is an perfect square of " + i + "\n");
}
}
public static void Main(String[] args) {
MyNumber obj = new MyNumber();
//Test case
obj.perfect_square(16);
obj.perfect_square(31);
obj.perfect_square(81);
obj.perfect_square(25);
}
}
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

```
# Python 3 Program
# Check if a number is perfect square
class MyNumber :
def perfect_square(self, number) :
status = 0
i = 1
while (i <= number / 2 and i * i <= number) :
if (i * i == number) :
#When number is perfect square
status = 1
break
i += 1
if (status == 0) :
print(number ," Is not a perfect square ")
else :
print(number ," Is an perfect square of ", i )
def main() :
obj = MyNumber()
obj.perfect_square(16)
obj.perfect_square(31)
obj.perfect_square(81)
obj.perfect_square(25)
if __name__ == "__main__":
main()
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

```
# Ruby Program
# Check if a number is perfect square
class MyNumber
def perfect_square(number)
status = 0
i = 1
while (i <= number / 2 and i * i <= number)
if (i * i == number)
#When number is perfect square
status = 1
break
end
i += 1
end
if (status == 0)
print(number ," Is not a perfect square \n")
else
print(number ," Is an perfect square of ", i ,"\n")
end
end
end
def main()
obj = MyNumber.new()
obj.perfect_square(16)
obj.perfect_square(31)
obj.perfect_square(81)
obj.perfect_square(25)
end
main()
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

```
/*
Scala Program
Check if a number is perfect square
*/
import scala.util.control.Breaks._
class MyNumber {
def perfect_square(number: Int): Unit = {
var status: Boolean = false;
var i: Int = 1;
breakable {
while (i <= number / 2 && i *i <= number) {
if (i *i == number) {
//When number is perfect square
status = true;
break;
}
i += 1;
}
}
if (status == false) {
print(s"$number Is not a perfect square \n");
} else {
print(s"$number Is an perfect square of " + i + "\n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
obj.perfect_square(16);
obj.perfect_square(31);
obj.perfect_square(81);
obj.perfect_square(25);
}
}
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

```
/*
Swift 4 Program
Check if a number is perfect square
*/
class MyNumber {
func perfect_square(_ number: Int) {
var status: Int = 0;
var i: Int = 1;
while (i <= number / 2 && i * i <= number) {
if (i * i == number) {
//When number is perfect square
status = 1;
break;
}
i += 1;
}
if (status == 0) {
print(number ," Is not a perfect square");
} else {
print(number ," Is an perfect square of ", i );
}
}
}
func main() {
let obj: MyNumber = MyNumber();
obj.perfect_square(16);
obj.perfect_square(31);
obj.perfect_square(81);
obj.perfect_square(25);
}
main();
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

```
<?php
/*
Php Program
Check if a number is perfect square
*/
class MyNumber {
function perfect_square($number) {
//Use to check perfect square
$status = 0;
$i = 1;
for ($i = 1; $i <= $number / 2 && $i *$i <= $number; ++$i) {
if ($i *$i == $number) {
//When number is perfect square
$status = 1;
break;
}
}
if ($status == 0) {
echo($number ." Is not a perfect square \n");
} else {
echo($number ." Is an perfect square of ". $i ."\n");
}
}
};
function main() {
$obj = new MyNumber();
//Test case
$obj->perfect_square(16);
$obj->perfect_square(31);
$obj->perfect_square(81);
$obj->perfect_square(25);
}
main();
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

```
/*
Node Js Program
Check if a number is perfect square
*/
class MyNumber {
perfect_square(number) {
//Use to check perfect square
var status = 0;
var i = 1;
for (i = 1; i <= parseInt(number / 2) && i *i <= number; ++i) {
if (i *i == number) {
//When number is perfect square
status = 1;
break;
}
}
if (status == 0) {
process.stdout.write(number + " Is not a perfect square \n");
} else {
process.stdout.write(number + " Is an perfect square of " + i + "\n");
}
}
}
function main() {
var obj = new MyNumber();
//Test case
obj.perfect_square(16);
obj.perfect_square(31);
obj.perfect_square(81);
obj.perfect_square(25)
}
main();
```

#### Output

```
16 Is an perfect square of 4
31 Is not a perfect square
81 Is an perfect square of 9
25 Is an perfect square of 5
```

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