Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
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

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming