如何输入正数并打印出平方根,直到输入的数字

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

本文关键字:输入 平方根 打印 数字 何输入      更新时间:2023-10-16

我找不到一个数字的sqrt根。 我的任务是 编写一个程序,提示用户输入正数,并打印出从 1 到输入数字的所有数字的平方根。这是我的代码

#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);
}

遇到的问题是我只能找到我输入 4 次的数字的 sqrt,而不是那个数字。 我的输出是 2222。我需要我的输出看起来像这样

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

你得到的正是你所要求的:

cout<< sqrt(x);

那是x的平方根,你说是4,因此答案总是2。你想要i.