Stuck on simple question

i am having trouble finding the sqrt roots for a number. my assignment is to Write a program that prompts the user for a positive number and prints out the square roots of all the numbers from 1 to the entered number.

the problem i have is that i can only find the sqrt of the number i inputted and not up to that number

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x;
cout <<"Enter how many numbers you wish to process:";
cin >> x;
if (x <= 0)
cout <<"Error negative value was entered";
else
cout << ":  " << sqrt(x) << endl;
return 0;
}


if i enter 4 i only get the sqrt of 4 and not the sqrts of 1,2,3 and 4

any help is appreciated
You need to write a loop that starts at y=1 and ends at y=x, and write the sqrt of y in the loop body.
That's because you are just taking the square root of said number. Consider a for loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x;
int i;

cout <<"Enter how many numbers you wish to process:";
cin >> x;

for(int i=1;i<=x;i++)

cout<< sqrt(x);

}


i did this know i am only getting the sqrt of 4 4 times? can someone correct my for loop please
You're almost there: You have i going from 1 to x, but then you take the square root of x. Try replacing x with i.
got it to work thanks for anyone in the future here is the correct code

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x;
int i;

cout <<"Enter how many numbers you wish to process:";
cin >> x;

for(int i=1;i<=x;i++)

cout<<<< sqrt(i)<<endl;

}
Topic archived. No new replies allowed.