i want code for finding square root using mental arithmetic

Pages: 12
Jan 10, 2011 at 5:05pm
i want code for finding square root using mental arithmetic
Jan 10, 2011 at 5:06pm
Great!
Jan 10, 2011 at 5:07pm
please anyone can tell me this code i don't know how to calculate the square root using mental calculation
Jan 10, 2011 at 5:11pm
can you tell me what is the code for this?
Jan 10, 2011 at 5:18pm
Your question doesn't make sense. "Mental Arithmetic" means specifically that only the human brain is used. Therefore there can be no code. Even if it did, we don't provide fully coded answers to problems that sound like homework on this site.

Try to re-phrase your question and show us what effort you have put into solving it and you will get some help.
Jan 10, 2011 at 5:28pm
my task is to find the square roots using mental arithmetic. can you tell me what i have to do. i don't know how to put the mental arithmetic in a c++ program
Jan 10, 2011 at 5:28pm
there is no more discription for me
Jan 10, 2011 at 5:33pm
We're not going to simply do your homework for you. First, you must discover a way to calculate a square root. When you've got that, come back and we'll help you write code to do those steps.

If you don't care how you do it, the <cmath> library includes sqrt()
Jan 10, 2011 at 5:35pm
This is pretty sad. If you don't understand how to calculate the square root of something then you shouldn't be trying to learn programming.
Jan 10, 2011 at 5:44pm
ok that is it i know man here it is


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
cout << "Please enter a number: ";
double a;
cin >> a;
const double EPSILON = 1E-14;
double xnew = a;
double xold;
do
{
xold = xnew;
xnew = (xold + a / xold) / 2;
}
while (fabs(xnew - xold) > EPSILON);

cout << "The square root is " << xnew << "\n";
system("pause");
return 0;
}

and another one


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
cout << "Please enter a number: ";
double a;
cin >> a;
double answer = sqrt(a);
cout << answer << "\n";
system ("pause");
return 0;
}
but i'm confused about finding square root by mental arithmetic
Jan 10, 2011 at 5:45pm
give me some hints please i'm beginner
Jan 10, 2011 at 5:57pm
If you already have code, why are you asking for code?
Jan 10, 2011 at 6:19pm
Here is a way to approximate the square root:
Find the square root of x:

Start with a guess G
if G^2 is close to x, stop, return G
else get a new G by taking the quotient of (G+x/G)/2
Repeat

One of your code snippets makes an attempt at it, but you need to tweak the algo for an exact match.
Jan 10, 2011 at 6:44pm
i didn't understand what are you saying
Jan 10, 2011 at 6:53pm
Take the number you want to know the square root of. Call this number x.

Guess a number that is smaller than x. Call your guess G.

Now square G, to find "G squared".

If "G squared" is equal to x, G is the square root of x. Stop.

If "G squared" is not equal to x, get a "new G" like so: New guess = (G + x/G) / 2

Take your new guess, go back three steps, carry on from there.
Jan 10, 2011 at 7:03pm
You can also use continued fractions.
Jan 10, 2011 at 7:13pm
thanks but is this idea fulfil my task?
Jan 10, 2011 at 7:15pm
How about Taylor Polynomials?
Last edited on Jan 10, 2011 at 7:16pm
Jan 10, 2011 at 7:33pm
is this code is ok aur not for mental arithmetic

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
cout << "Please enter a number: ";
double a;
cin >> a;
double answer = sqrt(a);

cout << "Please enter a number to guess that the square root of the previous number is equal to your guess: ";
double g;
cin >> g;
cout <<"The square root of the first entered number is :" << answer << "\n";
if(a = g*g) cout <<"The square root of your guessed number is approximately equal to the square root of the pervious number"<< "\n"<< "\n";
else cout << "The square root of your guessed number is not equal to the square root of the previous number";
cout << "\n";
system ("pause");
return 0;
}
Jan 10, 2011 at 8:15pm
"Mental arithmetic" is not some technical coding term. It means "inside your own head". How do you find a square root inside your own head, without calculator or writing anything down? That ios mental arithmetic.
Pages: 12