473,585 Members | 2,596 Online

# perfect square root

10 New Member
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++
Dec 18 '07 #1
12 20743
sicarie
4,677 Recognized Expert Moderator Specialist

Do you know how to find if a number is a perfect square root by hand?
Dec 19 '07 #2
Thana1995
10 New Member
i think it is sqrt but i'm really new to dis stuff
Jan 1 '08 #3
oler1s
671 Recognized Expert Contributor
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?
Jan 1 '08 #4
Thana1995
10 New Member
if it is a whole number then it is perfect square
but if it is a decimal then it is not a perfect square
Jan 1 '08 #5
oler1s
671 Recognized Expert Contributor
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.
Jan 1 '08 #6
Andr3w
42 New Member
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

Expand|Select|Wrap|Line Numbers
1. ...deleted...
2.
Hope this helped ;)
Jan 2 '08 #7
Thana1995
10 New Member
int number
cout<<"Please enter a number."<<'\n';
cin>>number
while number=sqrt(num ber)

that all i know
Jan 9 '08 #8
oler1s
671 Recognized Expert Contributor
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.
Jan 9 '08 #9
Thana1995
10 New Member
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"
Jan 19 '08 #10