305,134 views
19 votes
19 votes
Using while loop, write a C++ program that will compute and display the square roots of the first 25 odd positive integers starting from 1.

User Joepetrakovich
by
2.8k points

1 Answer

14 votes
14 votes

#include <iostream>

#include <cmath>

int main() {

int i = 1; // initialize counter to 1

while (i <= 25) { // loop until counter reaches 25

std::cout << "The square root of " << i << " is " << sqrt(i) << std::endl;

i += 2; // increment counter by 2 to get the next odd number

}

return 0;

}

User Paulius Dragunas
by
2.4k points