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);
}
/* Program Heading */
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.