# Thread: Help With Based Square Root Program...

1. ## Help With Based Square Root Program...

Hey Guys,

I'm taking my first Programming Class, and i'm having problems on my current lab...

I'm suppose to ask the user to enter a number, and then print all the even squares between 1 and that number......

I can do the basic structure, asking the program to cycle through 1 and 'n', but for the life of me I can't figure out how to get the program to output all the squares that lie between their....

Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{

int i;

printf("Enter an Integer: ");
scanf("%d", &n);

if (n>0)
{
for(i=1;i<=n;i++) //cycle through 1 to n
{

}   //set of...
}   //...nested brackets

else
{
printf("Sorry, try inputting a positive number next time.\n");
}

return 0;
}```

2. You've already included math.h, so take that as a hint and use the pow function. Or you could just do integer multiplication (i * i) to get the square...

To check if a number is even, use the modulus operator or bitwise operations (number & 1 == 0).

Code:
```if(number % 2 == 0)
{
/* It's even /*
}
else
{
/* It's odd /*
}```

3. Is the program supposed to do something like this? Say we enter "16", it would output the square roots of 4 and 16, which are 2 and 4 respectively.

Since you already know you need to use functions in "math.h" (you've already included it), then use sqrt sqrt - C++ Reference and modulus to check if the sqrt is even fmod - C++ Reference. Im sure if you search around you'll find examples of how to determine if a number is even.

When you use functions in "math.h" you have to "link" to that library. How you do this depends on your compiler, if your using gcc then pass "-lm" as an argument when you compile your program, i.e.: "gcc -lm myprogram.c -o myoutputfile", and run "myoutputfile".

EDIT: Also, you can skip all odd numbers, as their square root is never even. So you can count from i = 2 to n, and increment i by 2 each iteration (instead of by one by using "i++")

4. hey,

ahhh i should have been more detailed in my explanation...

the program should output all the possible squares between 1 and the number entered...
so if the user enters 100

the output should be
4
16
36
64
100

5. Yes, see my post.

For starters, modify your program to at least print the numbers from 1 to n. Then once you have, instead of printing the number, print its square root. Then, instead of always printing its square root, print it only if its square root is even (see above). Finally, as mentioned in my previous post, you can skip all odd numbers, since their square root is odd, so you cut down the number of iterations in half.

6. wow im having so much difficulty here, I tried to rethink the entire code based on the suggestions made, but i'm so new to this language i'm getting nowhere....how far off am i?

Code:
```#include < stdio.h >
#include < conio.h >

int main()
{

int i=0;
int n=0;

printf("Enter number: \t");
scanf("%f", &i);
for(i=1;i<=n;i++);
while( (i*i) < 0 ) i;
{
printf("%d ", i*i);
i++;
}
getch();

}```

7. Take exactly what you had in your initial code ( post#1 Help With Based Square Root Program...) and do what I suggested here (post#5 Help With Based Square Root Program...), in that order: first print the numbers in the for loop (all you do is add one print statement inside the for loop). Next, do the second step I suggested in that post (modify that single line you added in the first step). Let us know when you get that at least.

EDIT: I just noticed in the code you first posted, you need to declare "int n;", do it after the "int i;" line.

8. thanks for the quick reply, really appreciated, giving it a go...

9. sorta like this?

Code:
```#include <stdio.h>
#include <math.h>
#include <conio.h>

int main(void)
{

int i;
int n;

printf("Enter an Integer: ");
scanf("%d", &n);

if (n>0)
{
for(i=1;i<=2;) //cycle through 1 to n
printf ("The numbers are:",n);
{

}   //set of...
}   //...nested brackets

else
{
printf("Sorry, try inputting a positive number next time.\n");
}

}```

10. Not quite. You changed more than just that line, your for loop was fine before. You still need to go from "i = 1; i <= n; i++". Also
Code:
`printf ("The numbers are:",n);`
should be
Code:
`printf ("The numbers are: %d",i);`
After you have this, print the square root of "i", instead of "i" itself. See my first post for the link to "sqrt" which has an example at the bottom.

11. I keep getting "error C2668: 'sqrt' : ambiguous call to overloaded function"
but i see how I need to call the sqrt of i, i feel im getting closer lol :P

Code:
```#include <stdio.h>
#include <math.h>
#include <conio.h>

int main(void)
{

int i;
int n;

printf("Enter an Integer: ");
scanf("%d", &n);

if (n>0)
{
for(i=1;i<=n;i++) //cycle through 1 to n
printf ("The numbers are: %d",i);

{
i = (sqrt (i));
printf ("\nsqrt = %lf\n",i);

}   //set of...
}   //...nested brackets

else
{
printf("Sorry, try inputting a positive number next time.\n");
}

}```

12. I keep getting "error C2668: 'sqrt' : ambiguous call to overloaded function"
I dont get any errors when I compile. I imagine the problem is that "sqrt" is overloaded, and the way your calling it, the compiler doesnt know which version of "sqrt" to call. Basically, casting "i" to some type should fix the problem. I.e., do something like "sqrt( (double) i)" so it knows to use the version of "sqrt" that takes argument of type "double" (or cast to float, doesnt matter in this case).

Next (keeping in mind the error mentioned above) your for loop does this:
Code:
```for(i=1;i<=n;i++) //cycle through 1 to n
printf ("The numbers are: %d",i);```
But you probably wanted everything inside the for loop, like
Code:
```        for(i=1;i<=n;i++) //cycle through 1 to n
{
printf ("The numbers are: %d",i);
i = (sqrt (i));
printf ("\nsqrt = %lf\n",i);
}```
Next (again keeping in mind the error mentioned above) you dont want to do
Code:
```			i = (sqrt (i));
printf ("\nsqrt = %lf\n",i);```
. Because your changing "i", which is only supposed to change at the end of each iteration, by 1, which is handled by the "i++" part in the for loop--dont change that. You want to create a different variable, so do something like:
Code:
```        for(i=1;i<=n;i++) //cycle through 1 to n
{
float result = sqrt (i); // cast i to the appropriate type if you get errors
printf ("\nsqrt(%d) = %f\n", i, result); // (1)
}```
And finally, instead of always printing this (the line with (1)), you want to check if the result is even. See my first reply for the "fmod" function. Basically, a number is even if its division by 2 is 0, which is the same thing as its modulo 2 being 0 (i.e. fmod of result by 2 being 0).