Skip to content

Commit b206168

Browse files
Create Day 9 Valid Perfect Square.cpp
1 parent e0ec6c4 commit b206168

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Day 9 Valid Perfect Square.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
PROBLEM:
2+
3+
4+
Given a positive integer num, write a function which returns True if num is a perfect square else False.
5+
6+
Note: Do not use any built-in library function such as sqrt.
7+
8+
Example 1:
9+
10+
Input: 16
11+
Output: true
12+
Example 2:
13+
14+
Input: 14
15+
Output: false
16+
17+
18+
19+
SOLUTION:
20+
21+
22+
class Solution {
23+
public:
24+
bool isPerfectSquare(int num) {
25+
26+
long long i,a,b;
27+
28+
for(i=1;i*i<=num;i++)
29+
{
30+
if(num%i==0)
31+
{
32+
a=num/i;
33+
b=i;
34+
35+
if(a==b)
36+
return true;
37+
}
38+
}
39+
40+
return false;
41+
}
42+
};

0 commit comments

Comments
 (0)