perfect square root

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thana1995
    New Member
    • Dec 2007
    • 10

    perfect square root

    i need help mto do this question i have no clue on how to dis dis question

    Write a program that checks whether the number entered by the user is a perfect square or not.
    pls help

    and does anybody know where i could get help on c++
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    We can help you with C++.

    Do you know how to find if a number is a perfect square root by hand?

    Comment

    • Thana1995
      New Member
      • Dec 2007
      • 10

      #3
      i think it is sqrt but i'm really new to dis stuff

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        What is what sqrt?

        It's 1900. No computers exist in the world. You are in a room with s sheet of paper and a number and its square root on it. How do you know if the number is a perfect square?

        Having trouble thinking about it so abstractly? Let me give you four examples.

        Example 1: 36 and 6.
        Example 2: 30 5.47722558
        Example 3: 25 and 5
        Example 4: 24 and 4.89897949

        So. How do you know if a number is a perfect square?

        Comment

        • Thana1995
          New Member
          • Dec 2007
          • 10

          #5
          if it is a whole number then it is perfect square
          but if it is a decimal then it is not a perfect square

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            Ok, now look at the concept of an int in C++. An integer (which corresponds to its mathematical equivalent). 5 is an integer. 5.5 is not.

            So in my example, 36 has a square of 6. This fits into an integer. 6 squared is 36.
            30 has a square root of 5.48. What happens when you put it into an integer? Well, the five part works out. The decimal portion gets discarded. So in integer arithmetic, 30 has a square root of 5. And the square of 5 is 25. Whoops. 25 and 30 are not equal.

            Do you see how to write the code now? If the code looks confusing, write down the logic in plain english first.

            Comment

            • Andr3w
              New Member
              • Nov 2007
              • 42

              #7
              Well, there is another way to do that. You could use modf and break the fraction in 2 pieces, thus saving a multiplication. Now in order to get the perfect square you could just check if the decimal part was equal to zero. The following code shows that

              [code=cpp]
              ...deleted...
              [/code]

              Hope this helped ;)
              Last edited by RedSon; Jan 9 '08, 10:04 PM. Reason: NO!

              Comment

              • Thana1995
                New Member
                • Dec 2007
                • 10

                #8
                int number
                cout<<"Please enter a number."<<'\n';
                cin>>number
                while number=sqrt(num ber)


                that all i know

                Comment

                • oler1s
                  Recognized Expert Contributor
                  • Aug 2007
                  • 671

                  #9
                  That's not complete compilable code. Those aren't statements anyway, as you are missing semicolons. Your while loop...well, I don't see how a while loop is useful, but your while loop statement is nonsensical. And you forgot CODE tags. Not that it matters since what you posted is utter nonsense.

                  All forgiveable, as you are a complete beginner. But I see no effort to think on your part. Break it down into sections. Start with a code skeleton. You need a main. You probably want the iostream header for I/O. You probably want cmath for the sqrt function.

                  Have you covered if statements? You seem completely unfamiliar with the syntax. Fix that problem. Look through your book. Or go online, like cprogramming.co m and cplusplus.com and look at the syntax again.

                  If you find it difficult to write code directly, write PLAIN ENGLISH statements, detailing the steps the program must take. Then try and convert them into code.

                  Comment

                  • Thana1995
                    New Member
                    • Dec 2007
                    • 10

                    #10
                    please enter a number
                    input number
                    square root number
                    if number has decimal output " it is not a perfect square"
                    if number has no decimal output"perfect square"

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      Good. How are you going to determine if a number has a decimal portion? This will be the trickiest part of your assignment, but it does look like it will be the last hurdle to overcome.

                      oler1s gave you a major hint in post number 6 - see if you can use this.

                      Comment

                      • Thana1995
                        New Member
                        • Dec 2007
                        • 10

                        #12
                        that the part i can't seem to figure out

                        Comment

                        • Adrian Valerio
                          New Member
                          • Aug 2010
                          • 1

                          #13
                          Thanks oler1s for the hint. This is the code I was finally able to write in order to determine if a number has a square root or not.

                          This is my third day learning by my self algorithms and C so I dont know if there is a better/easier way to solve the problem but... I hope it helps.

                          /*----------------------------*/
                          /* This code determines if an */
                          /* integer introduced by the */
                          /* user has a perfect square */
                          /* root or not. */
                          /* */
                          /* Written by: Adrian Valerio */
                          /*----------------------------*/

                          #include <stdio.h>
                          #include <math.h>

                          int main(void)

                          {
                          int number;
                          double result;

                          printf ("\n Introduce an integer: ");

                          scanf ("%i", &number);

                          result= sqrt (number);

                          if ((result * result)== number)
                          printf ("\n The integer HAS a perfect square \n\n");

                          else
                          printf ("\n The integer DOES NOT HAVE a perfect square \n\n");

                          getch ();
                          }

                          Comment

                          Working...