gpt4 book ai didi

c++ - 使用完全正方形的计数器控制 While 循环

转载 作者:行者123 更新时间:2023-11-28 07:11:54 25 4
gpt4 key购买 nike

我是编程新手,我的老师并没有真正教我们东西。我需要编写一个程序,使用 pow 函数找到前 20 个完全正方形。需要成为一个计数器控制的循环。我真的不知道那是什么意思,但我在下面尽了最大的努力。请帮助我!

#include <iostream>
#include <stdio.h>
#include <cmath>

using namespace std;

int main ()
{
int a, b;
cout << "Enter the num:";
scanf("% d", &a);
b = sqrt (a);
if ((b * b) == a)
cout << "The given number is a perfect square";
else
cout << "The given number is not a perfect square";
getch();
}

最佳答案

如果我理解正确,那么计数器控制循环意味着它应该是一个具有变量(计数器)和条件(用于循环)的循环,并且当变量回答它应该循环的条件时。在您的示例中,整数 i 将作为计数器,循环将是 for 循环:

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
for(int i = 1; i < 21; i++)
{
cout << i << "'s perfect square is " << pow(i, 2) << endl;
}
}

关于c++ - 使用完全正方形的计数器控制 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20844777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com