Write a function named nextPerfectSquare that returns the first perfect square that is greater than its integer argument. A perfect square is an integer that is equal to some integer squared. For example 16 is a perfect square because 16 = 4 * 4. However 15 is not a perfect square because there is no integer n such that 15 = n*n.
The signature of the function is
int nextPerfectSquare(int n)
Examples
Answer Coming Soon. You can also Guess in comments.
The signature of the function is
int nextPerfectSquare(int n)
Examples
Answer Coming Soon. You can also Guess in comments.
static int nextPerfectSquare(int n)
ReplyDelete{
int temp=0;
if(n<0)
temp=0;
else if (n==0)
temp=1;
else
{
for(int i=1;i<=n;i++)
{
temp=i*i;
if(temp>n)
break;
}
}
return temp;
}
public static void main(String asgs[])
{
nextPerfectSquare(-5);
nextPerfectSquare(135);
nextPerfectSquare(1);
nextPerfectSquare(0);
nextPerfectSquare(6);
nextPerfectSquare(36);
}
This comment has been removed by the author.
ReplyDeleteimport java.util.Scanner;
ReplyDeletepublic class NextPerfectSquare{
public static void main(String [] args){
Scanner cin = new Scanner(System.in);
System.out.println("Enter number");
int num = cin.nextInt();
System.out.println(next(num));
}
public static int next(int n){
int count=0;
for(int i=0; i<=n;i++){
if(i*i==n){count =i+1; break; }
if(i*i>n){count= i; break;}
}
return count*count;
}
}
public class Nextperfectsquare {
ReplyDeletepublic static void main(String[] args) {
int result = nextperfectsquare(6);
System.out.println("nextperfectsquare->" + result);
}
public static int nextperfectsquare(int n) {
int i = 0, square = 0;
if (n < 0) {
return 0;
}
while (square <= n) {
square = i * i;
i = i + 1;
if (square == n) {
return i * i;
}
if (square > n) {
return square;
}
}
return square;
}
}