# Thread: Help with Perfect Squares before and after

1. ## Help with Perfect Squares before and after

Hello I am new to C programming and am simply doing it for fun. I'm trying to write a program that asks the user for a number and tells them if it's a perfect square or not, and then what the perfect square before and after is. Here is what I have so far:

Code:
```    int x, y;    int temp = y;

/*
int loopCounter;
int nextPerfectSquare;
*/
/*Sub Method- Takes y as parameter*/
int perfectSquares(y)
{
x = sqrt(y);
/*See if x times x is y*/
return (x * x == y);
}

printf("Perfect Squares\n");
printf("---------------\n\n");
printf("Enter a positive integer (1-1000000):\n");

/*The check if number entered is a perfect square*/
scanf("%d", &y);

/*fgets(&y, maxValue, stdin);*/

/*Test print*/
printf("VALUE ENTERED: %d\n",y);

if(perfectSquares(y))
{
printf("\n");
printf("The number you entered is a perfect square!\n");
}

else
{

printf("The number you entered is not a perfect square!\n");

}

/*Print Perfect Square Before and After*/

int loopCounter = 1;
for(loopCounter =1; loopCounter<y; loopCounter++)
{
y--;
if(perfectSquares(y))
{
printf("\nPerfect square before: %d \n",y);

}
}```
The program works correctly in telling the user if it's a perfect square or not but I'm not sure how to fix my loop so it only says 1 perfect square before and 1 after. Can someone please help me? Also how can I use fgets instead of scanf and still get it to work? Thank you for your time.

2. You will need two loops, one for before and one for after. Start loopCounter at y and go down (before) or up (after) until you find the first perfect square.

3. Is this correct?
Code:
``` /*Print Perfect Square Before and After*/

int loopCounter = y;
int i=0;
for(i=0; i<loopCounter; i++)
{
y--;
if(perfectSquares(y))
{
printf("\nPerfect square before: %d \n",y);

}
}

for(i=0; i<loopCounter; i++)
{
y++;
if(perfectSquares(y))
{
printf("\nPerfect square after: %d \n",y);

}
}

}```
Sorry if I misunderstood. Why does it print heaps of perfect squares before instead of just 1 before? Also my after is glitched xD

4. It would probably be easier to demonstrate in a while loop, now that I think about it. Try something along these lines:
Code:
```loopCounter = y;
while loopCounter is not a perfect square
loopCounter--;
print "perfect square before is" loopCounter```
After is similar, but loopCounter needs to increment, not decrement.

5. Would the correct syntax be like this?
Code:
```int loopCounter = y;
while(perfectSquares(loopCounter) ==FALSE){

loopCounter --;
printf("\nPerfect square before: %d \n",y);
}```
Or this?

Code:
```int loopCounter = y;
while(perfectSquares(!loopCounter)){

loopCounter --;
printf("\nPerfect square before: %d \n",y);
}```

6. The first loop condition would work, so long as you have FALSE defined as 0 somewhere. The version using ! would look like this (this is how I would do it):
Code:
```while (!perfectSquares(loopCounter)) {
loopCounter--;
}
// Notice this is outside the loop, as my indentation showed in the above pseudo code
// Also notice, I am printing loopCounter, not y (remember, y is the number the user entered)
printf("Perfect square before: %d\n", loopCounter);```

7. Thanks, I got it working. I would appreciate it if you could answer one more quick question, how can I replace
Code:
` scanf("%d", &y);`
with an fgets statement?

8. Fgets() input into a temporary (stack) char buffer, and then use atoi() to get the number.

9. Why would you want to?
If you really do want to, you'd have to read the line into a string then use e.g. atoi to extract the integer.