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
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.