# A simple square root calculator

This is a discussion on A simple square root calculator within the C Programming forums, part of the General Programming Boards category; Hello all my dear friends, This is a very simple calculator I made to find the square root of an ...

1. ## A simple square root calculator

Hello all my dear friends,

This is a very simple calculator I made to find the square root of an entered number.

It works fine and errors free

The calculator keeps running until the user enter 0 to quit the program

I hope the code is free of mistakes

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

int main()
{
float x;

do {
printf("Square Root Calculator\n\n");
printf("\nNote:\nThe square root of 1 is 1, and 0 is 0\n\n");

system("pause");
system("cls");

printf("\nEnter a number to find the sqauare root, 1 to go back to main or 0 to exit:\n");

scanf("%f", &x);

if(x != 0 && x > 1){

x = sqrt(x);

printf("\n\n\nThe sqaure root of the entered number is %.1f\n\n\n", x);
}

else if(x == 1){

system ("cls");
continue;

}
else if(x == 0){
exit(0);
}

system ("pause");
system ("cls");

} while (x != 0);

}```

Thank you

2. What happens if I input 0.1?

3. Originally Posted by claudiu
What happens if I input 0.1?
You are right, it brings the user to the main screen.

so the new code after correcting (Bold):

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

int main()
{
float x;

do {
printf("Square Root Calculator\n\n");
printf("\nNote:\nThe square root of 1 is 1, and 0 is 0\n\n");

system("pause");
system("cls");

printf("\nEnter a number to find the sqauare root, 1 to go back to main or 0 to exit:\n");

scanf("%f", &x);

if(x != 0 && x != 1){

x = sqrt(x);

printf("\n\n\nThe sqaure root of the entered number is %.1f\n\n\n", x);
}

else if(x == 1){

system ("cls");
continue;

}
else if(x == 0){
exit(0);
}

system ("pause");
system ("cls");

} while (x != 0);

}```

Thank you for your note, claudio

4. Why do you explicitly look for x==1? What's the problem with including that in the general conditions? sqrt(1) is 1.

Also, ideally your stopping condition should be something like x = -1 or x <0. 0 also has a valid sq root. It's 0.

5. Originally Posted by claudiu
Why do you explicitly look for x==1? What's the problem with including that in the general conditions? sqrt(1) is 1..

Also, ideally your stopping condition should be something like x = -1 or x <0. 0 also has a valid sq root. It's 0.
I already gave the user a note telling them that the square root of 0 and 1, because the program will use 0 and 1 to do functions like 1 to go back and 0 to exit.

I will try to make some changes as in your reply above and post the code again

Now, it's too late here in malaysia (3:21 AM) and I have classes tomorrow,

so thank you claudio, and good night

6. here i my code find squareroot without math.h

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

int main(){

int i;
double number,x;

printf("\nenter number:");
scanf("%lf",&number);

x=20;

for(i=0;i<1000;i++){
x=0.5*(x+(number/x));

}
printf("\nresult:%f\n",x);
return 0;

}```

7. O_o... Where did you find that code?!

8. Originally Posted by Sipher
O_o... Where did you find that code?!

i wrote with using this sequence

http://img138.imageshack.us/img138/1696/90511389.jpg

if you want to find exactly result, you have to find limit, but 1000 loop is also give very very close answer