#include <iostream>
usingnamespace std;
int main()
{
cout.precision(36);
cout << "Anmol's Square Root Calculator!" << endl;
cout << "" <<endl;
cout << "This program will compute the square root\n";
cout << "of a number using the Babylonian algorithm!\n";
cout <<""<<endl;
cout <<"Only positive numbers work with this algorithm!"<<endl;
cout <<""<<endl;
cout <<""<<endl;
cout <<"The algorithm just repeats the following formula as many times as needed\n""to get to the desired accuracy."<<endl;
cout <<""<<endl;
cout <<"x = (x + (n/x))/2"<<endl;
cout <<""<<endl;
cout <<"Where x is any estimate number of the sqrt\n""and n is the number you need the sqrt of."<<endl;
cout <<""<<endl;
cout <<"The program is programmed to always use 1 as the very first estimate."<<endl;
cout <<"The very first estimate can be any number but the closer it is to\n""the actual square root the algorithm takes less iterations in order to find the actual square root."<<endl;
cout <<""<<endl;
cout <<""<<endl;
cout <<""<<endl;
char playAgain='y';
while (playAgain !='n')
{
int count, cycle(1);
longdouble guess, sqrt, num;
cout << "Please enter the number you would like to compute the square root of: ";
cin >> num;
cout << "" << endl;
do{
if (num <= 0)
{
cout<<""<<endl;
cout<<"Invalid input. Please re-enter a valid number: ";
cin >> num;
cout<<""<<endl;
}
}
while (num <= 0);
cout<< "Please enter the number of times you would like to iterate the algorithm: ";
cin>>count;
do{
if (count <= 0)
{
cout<<""<<endl;
cout<<"Invalid input. Please re-enter a valid number: ";
cin >> count;
cout<<""<<endl;
}
}
while (count <= 0);
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<"Iteration #1 Approximate: 1"<<endl;
cycle++;
for (guess=1; count!=1; count--)
{cout<<""<<endl;
cout<<"Approximate #"<<cycle<<" = ("<<guess<<" + ("<<num<<" / "<<guess<<")) / 2."<<endl;
guess =(guess + (num/guess))/2;
cout<<""<<endl;
cout<<"Iteration #"<<cycle<<" Approximate: "<<guess<<""<<endl;
sqrt = guess;
cycle++;
}
cout<<""<<endl;
cout <<""<< endl;
do { cout <<""<<endl;
cout << "Would you like to calculate again? (y/n): ";
cin >> playAgain;
cout <<" "<<endl;
do { cout <<""<<endl;
if ((playAgain !='y' && playAgain !='n'))
{
cout << "Invalid input. Only y/n: ";
cin >> playAgain;
cout <<" "<<endl;
} }while (playAgain !='y' && playAgain !='n');
} while (playAgain !='y' && playAgain !='n');
}
cout<<"Thank you for using a program made by Anmol Sethi!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<"Hope you use it again!"<<endl;
cout<<""<<endl;
cin.get();
return 0;
}
Thats my algorithm program :D. It uses the babylonian algorithm to find the approximate square root of the number you enter. Can I get any feed back on what to improve and how?
Use consistent indentation.
Use whitespace to increase readability.
Using std::endl should only be used when you need to flush the output stream.
Refactoring code into functions would be good.
For most situations, inserting a newline into the output stream is more appropriate (and a much less expensive operation on buffered streams.)
One could rewrite:
1 2 3 4 5
cout<<"Thank you for using a program made by Anmol Sethi!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<"Hope you use it again!"<<endl;
cout<<""<<endl;
as:
1 2
cout << "Thank you for using a program made by Anmol Sethi!\n\n\n";
cout << "Hope you use it again!\n\n" ;
Or even as:
1 2
cout << "Thank you for using a program made by Anmol Sethi!\n\n\n""Hope you use it again!\n\n" ;