Sqrt Function

closed account (S3TkoG1T)
Hello!

So I am having some trouble coming up with a code that can prompt the user to enter a positive number and prints out the square roots of all numbers from 1 to the entered number. Here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

  1 #include <iostream>
  2 #include <cmath>
  3 using namespace std;
  4 
  5 int main () {
  6 
  7 double input; // users input
  8 double result; 
  9 
 10         cout << "Please Enter a positive number " << endl;
 11                 cin >> input;
 12                 result = sqrt(input);
 13                 cout << endl << "Your sqrt = " << result << endl;
 14                 
 15 return 0;       
 16 
 17 }


My Question is: any Advice on how can I calculate all the other positive numbers from 1 to whatever number the user input?

Thanks!
You can use a for loop:

1
2
3
4
5
cout << "Please Enter a positive number " << endl;
cin >> input;

for (double i = 1.0; i <= input; i += 1.0)
    cout << "Your sqrt = " << sqrt(i) << endl;
closed account (S3TkoG1T)
You're amazing! Thank you
Topic archived. No new replies allowed.