Square Root using Recursive Function
/*
* SquareRoot.cpp
*
* Created on: Dec 2, 2010
* Program Description:
* This program would calculate the square root of a number entered by user using
* recursive function as well as the non recursive version of it.
*/
#include<iomanip>
#include<cmath>
#include<iostream>
using namespace std;
const double tol = 1.0E-4; // Tolerance constant.
// Function Prototypes
double sqrRootRecursive(double, double, double);
double sqrRootNonRecursive(double, double, double);
// Function Definitions
double sqrRootRecursive(double number, double approx, const double tol) {
// Using Recursive function to calculate Square Root of user input.
if (fabs (approx * approx - number) <= tol)
return approx;
else if (fabs((approx * approx) - number) > tol)
approx = sqrRootRecursive(number, (approx * approx + number) / (2 * approx), tol);
return approx;
}
double sqrRootNonRecursive(double number, double approx, double tol) {
// Using non recursive function to calculate Square Root of user input.
while(fabs(approx * approx - number) != tol && fabs(approx * approx - number) > tol) {
approx = (approx * approx + number) / (2 * approx);
}
return approx;
}
int main(void) {
double numInput; // Variable created to hold user input.
double approximation;
// Variable created to hold approximation so that if user inputs 0 then the
// Square Root of 0 is evaluated as 0 not 0.00781305
cout << endl << "----------------------";
cout << endl << "SQUARE ROOT CALCULATOR";
cout << endl << "----------------------\n";
cout << "Enter the number to get the Square Root: \n";
cin >> numInput; // User input stored in variable "numInput".
while(numInput < 0.0) {
// If the user inputs negative numbers than we prompt them to
// enter the positive number.
cout << endl << "Error!! Please Enter Positive Number: \n";
cin >> numInput;
}
approximation = numInput / 10000;
// To get the approximate value of the Square Root Operation.
double sqrRootRec = sqrRootRecursive(numInput, approximation, tol);
// Square Root using recursive function is stored in variable "sqrRootRec".
cout << endl << "Square Root of " << numInput << " using Recursive Function: "
<< sqrRootRec << endl; // Display
double sqrRootNonRec = sqrRootNonRecursive(numInput, approximation, tol);
// Square Root using non recursive function is stored in variable "sqrRootNonRec".
cout << endl << "Square Root of " << numInput << " using Non-Recursive Function: "
<< sqrRootNonRec << endl; // Display
return 0;
}
* SquareRoot.cpp
*
* Created on: Dec 2, 2010
* Program Description:
* This program would calculate the square root of a number entered by user using
* recursive function as well as the non recursive version of it.
*/
#include<iomanip>
#include<cmath>
#include<iostream>
using namespace std;
const double tol = 1.0E-4; // Tolerance constant.
// Function Prototypes
double sqrRootRecursive(double, double, double);
double sqrRootNonRecursive(double, double, double);
// Function Definitions
double sqrRootRecursive(double number, double approx, const double tol) {
// Using Recursive function to calculate Square Root of user input.
if (fabs (approx * approx - number) <= tol)
return approx;
else if (fabs((approx * approx) - number) > tol)
approx = sqrRootRecursive(number, (approx * approx + number) / (2 * approx), tol);
return approx;
}
double sqrRootNonRecursive(double number, double approx, double tol) {
// Using non recursive function to calculate Square Root of user input.
while(fabs(approx * approx - number) != tol && fabs(approx * approx - number) > tol) {
approx = (approx * approx + number) / (2 * approx);
}
return approx;
}
int main(void) {
double numInput; // Variable created to hold user input.
double approximation;
// Variable created to hold approximation so that if user inputs 0 then the
// Square Root of 0 is evaluated as 0 not 0.00781305
cout << endl << "----------------------";
cout << endl << "SQUARE ROOT CALCULATOR";
cout << endl << "----------------------\n";
cout << "Enter the number to get the Square Root: \n";
cin >> numInput; // User input stored in variable "numInput".
while(numInput < 0.0) {
// If the user inputs negative numbers than we prompt them to
// enter the positive number.
cout << endl << "Error!! Please Enter Positive Number: \n";
cin >> numInput;
}
approximation = numInput / 10000;
// To get the approximate value of the Square Root Operation.
double sqrRootRec = sqrRootRecursive(numInput, approximation, tol);
// Square Root using recursive function is stored in variable "sqrRootRec".
cout << endl << "Square Root of " << numInput << " using Recursive Function: "
<< sqrRootRec << endl; // Display
double sqrRootNonRec = sqrRootNonRecursive(numInput, approximation, tol);
// Square Root using non recursive function is stored in variable "sqrRootNonRec".
cout << endl << "Square Root of " << numInput << " using Non-Recursive Function: "
<< sqrRootNonRec << endl; // Display
return 0;
}
Comments
Post a Comment