|
Using a while Loop to Find the
Square
Roots of the First N Integers
% cat roots.cpp
// Square Roots
#include <iostream>
using namespace std;
#include <iomanip>
#include <cmath>
int main()
{
int count, MAXCOUNT;
cout << "Enter number of roots desired: ";
cin >> MAXCOUNT;
cout << "NUMBER SQUARE
ROOT\n";
cout << "------ -----------\n";
cout.setf(ios::showpoint );
cout.setf(ios::fixed);
cout.precision(3);
count = 1; // initialize
while ( count <= MAXCOUNT )
{
cout << setw(4) << count
<< setw(15)
<<
sqrt(count) << endl;
count = count + 1;
}
return 0;
}
% g++ roots.cpp
% a.out
Enter number of roots desired: 6
NUMBER SQUARE ROOT
------ -----------
1 1.000
2 1.414
3 1.732
4 2.000
5 2.236
6 2.449
|