Back to Home Page

/******************************************************************************
Problem: A method to calculate Q ^(1/n) is the follow:
B = ( (Q/A^(n-1)) + A*(n-1) )/ n
where:
Q = Real number(+) to evaluate
n = Integer(+)> 1
B = result if B=A
Process:
a) Assum A=1
b) Calculate B
c) if A = B then the result will be B
Otherwise assign B to A and go to (b)
Note: use condition fabs(A-B)<0.001

Course: C++
Tutor : Guillermo Julca
Mercy College

View Output
*******************************************************************************/

//---------------------------------------------------------------------------

#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <iomanip.h> // for setprecision
#pragma hdrstop
//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
int n;
double A,B,Q ;
bool finish;
char ans;
do
{
do
{
cout<<"Enter the n value : ";
cin>> n;
}while(n <= 1);
do
{
cout<<"Enter Q = ";
cin>>Q;
}while(Q < 0);
A=1;
do
{
B=( (Q/(pow(A,n-1))) + A*(n-1) )/ n;
if (fabs(A-B) < 0.001) // use fabs for double
finish = true;
else
{
finish = false;
A = B;
}// end else
} while (! finish);
cout<<"("<<setprecision(2)<<Q<<" )"<<"^ "<<"(1 / "<<n<<") = "<<setprecision(4)<<B<<endl;
cout<<"Continue(Y/N) ? :";
cin>>ans;
}while(ans=='Y' || ans=='y');
getchar();
return 0;
}

View Output
//---------------------------------------------------------------------------

Back to Home Page


Copyright  © 2002                                   GJ  GUILLERMO JULCA