Posted on by Kalkicode
Code Number

# Check if a number is perfect square

The problem we are trying to solve is to determine whether a given number is a perfect square or not. A perfect square is a number that can be expressed as the square of an integer. For example, 9, 16, and 25 are perfect squares since they can be expressed as 3^2, 4^2, and 5^2, respectively.

## Approach

To check if a number is a perfect square, we can use a simple algorithm that involves iterating through numbers starting from 1 and checking if the square of the current number equals the given number. We can stop iterating once we reach the number divided by 2, as there won't be any perfect squares greater than half of the number.

### Pseudocode:

``````function isPerfectSquare(number):
status = 0
i = 1
while i <= number/2 and i*i <= number:
if i*i == number:
status = 1
break
i = i + 1
if status == 0:
print number, "is not a perfect square"
else:
print number, "is a perfect square of", i```
```

## Explanation with Example

Let's understand the algorithm with an example. Suppose we want to check if the number 16 is a perfect square.

1. Initialize the status variable to 0.

2. Start iterating from 1 to the number divided by 2, i.e., from 1 to 8.

3. In the first iteration, i = 1. Check if 1^2 equals 16. Since it is not, continue to the next iteration.

4. In the second iteration, i = 2. Check if 2^2 equals 16. Since it is not, continue to the next iteration.

5. In the third iteration, i = 3. Check if 3^2 equals 16. Since it is not, continue to the next iteration.

6. In the fourth iteration, i = 4. Check if 4^2 equals 16. Since it is true, set the status variable to 1 and break out of the loop.

7. Check the status variable. Since it is 1, print "16 is a perfect square of 4."

This process can be repeated for other numbers as well.

## Code Solution

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``````

## Time Complexity

The time complexity of this algorithm is O(n/2) in the worst case, where n is the given number. This is because we iterate from 1 to n/2, checking if each number is a perfect square. In the best case, if the given number is a perfect square on the first iteration, the time complexity would be O(1).

## Output Explanation

Let's examine the output of the provided code:

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

The code checks if each given number is a perfect square using the algorithm we discussed. It correctly identifies that 16, 81, and 25 are perfect squares and provides the corresponding square roots (4, 9, and 5) as output. For the number 31, the code determines that it is not a perfect square.

Overall, the code successfully checks whether a given number is a perfect square or not.

## Comment

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.

Categories
Relative Post