#include <stdio.h>
#include <math.h>
long factcalc(int num1);
int main(void)
{
int num1;
long factorial;
int d;
int out;
printf("Please enter a number that is greater than 0");
scanf_s("%d", &num1);
if (num1 < 0) {
printf("Error, number has to be greater than 0");
} else if (num1 == 0) {
printf("\nThe answer is 1");
} else {
factorial = factcalc(num1);
printf("\nThe factorial of your number is\t %ld", factorial);
}
return 0;
}
long factcalc(int num1)
{
int factorial = 1;
int c;
for (c = 1; c <= num1; c++)
factorial = factorial * c;
return factorial;
}
我在想,如何讓程序不斷詢問用戶輸入,直到用戶輸入'-1'?因此,即使在計算了一個數字的階乘之後,它仍會要求更多的數字,直到用戶輸入-1,同樣,當它顯示錯誤消息等時也是如此。提前致謝。如何在沒有轉到語句的情況下返回到我的程序的開始
爲什麼不使用循環?我認爲不需要'goto'。 – ameyCU