john315
john315

Reputation: 27

How do I input a positive number and prints out the square roots up to the number entered?

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. Here is my 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(x);

}

the problem I have is that I can only find the sqrt of the number I inputted 4 times and not to that number. my output comes out at 2222. i need my output to look like this

Enter how many numbers you wish to process: 4
1: 1
2: 1.41421
3: 1.73205
4: 2

Upvotes: 1

Views: 973

Answers (1)

tadman
tadman

Reputation: 211680

You're getting exactly what you asked for:

cout<< sqrt(x);

That's the square root of x, which you've said was 4, ergo the answer is 2 always. You want i.

Upvotes: 3

Related Questions