468,463 Members | 1,988 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

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++
Dec 18 '07 #1
12 20139
sicarie
4,677 Expert Mod 4TB
We can help you with C++.

Do you know how to find if a number is a perfect square root by hand?
Dec 19 '07 #2
i think it is sqrt but i'm really new to dis stuff
Jan 1 '08 #3
oler1s
671 Expert 512MB
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
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 Expert 512MB
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
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
int number
cout<<"Please enter a number."<<'\n';
cin>>number
while number=sqrt(number)


that all i know
Jan 9 '08 #8
oler1s
671 Expert 512MB
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.com 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
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
Ganon11
3,652 Expert 2GB
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.
Jan 19 '08 #11
that the part i can't seem to figure out
Jan 19 '08 #12
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 ();
}
Aug 20 '10 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by cplusplus | last post: by
32 posts views Thread by priyam.trivedi | last post: by
4 posts views Thread by sathyashrayan | last post: by
4 posts views Thread by The 1 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.