473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 20725
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

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

Similar topics

4
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
2
by: Protoman | last post by:
Can you help me? For 4, my square root funct gives 4 instead of 2; here's the code: #include <iostream> #include <cstdlib> using namespace std; template<class T> T Abs(T Nbr) {
2
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get...
32
by: priyam.trivedi | last post by:
Hi! Could anyone tell me how to find the square root of a number without using the sqrt function. I did it by using Newton's Formula. How can it be done by using the Binomial Theorem/Taylor...
4
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
4
by: The 1 | last post by:
Q-1 Can sm1 suggest a program to check whether a given no is perfect square or not?? Q-2 Also sm1 suggest a program to check whether a given no is perfect power or not?? * PEfect power is a no...
10
by: socondc22 | last post by:
my program is trying to use the babylonian algorithm in order to find the square root... i have a number for the number to have the square root taken of and also a number to run the loop... Whenever...
4
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.