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. #1
    Registered User main()'s Avatar
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    14

    Post 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);
    
    }
    Please try it and give me your feedback


    Thank you

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What happens if I input 0.1?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User main()'s Avatar
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    14
    Quote Originally Posted by claudiu View Post
    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. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User main()'s Avatar
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    14
    Quote Originally Posted by claudiu View Post
    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. #6
    Registered User
    Join Date
    May 2010
    Posts
    10
    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. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    O_o... Where did you find that code?!
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    10
    Quote Originally Posted by Sipher View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  2. Checking for integer after square root
    By Boomba in forum C++ Programming
    Replies: 13
    Last Post: 01-04-2006, 10:22 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM
  5. C++ Beginner Simple Calculator
    By mom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 01:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21