【发布时间】:2023-04-05 20:15:02
【问题描述】:
我需要编写一个程序来计算从 0 到 n 的数字的平方。我知道如何使用迭代来做到这一点,但是如何使用递归来重写注释函数?我觉得这是一项简单的任务,我应该毫无问题地处理它,但是出了点问题。
#include <stdio.h>
#include<conio.h>
int main()
{
int x=0, n,m;
printf("Enter last integer ");
scanf_s("%d\n", &n);
while (x < n) /* How to rewrite this function using recursion?*/
{
printf("\n%d\n", x*x);
x++;
}
_getch();
return 0;
}
【问题讨论】:
-
"但是出了点问题" --- 什么?请展示你的努力。实际上,您是在向我们展示可以运行的代码,但在一些 other 代码中请求调试帮助。
-
指定你必须应用递归的部分。计算正方形和/或打印从 0 到 n 的正方形。