Here is how to do this with loops
int n=10; //This is your Number
int i = 0;
for(i=n;i>=1;i--)
if((int)Math.sqrt(i)*(int)Math.sqrt(i)==i)
break;
System.out.println(i);
Below is how it works:-
The Loop runs from the n
, which is your number, to 1
. It then checks whether, the square root of i
, which is running through n
to 1
, is a perfect square
. If it is, it breaks
the loop
, and prints the value of i
on the screen.
3
solved Finding largest square less than a given number in Java [closed]