C 仅使用主函数查找数字阶乘的程序

C 仅使用主函数查找数字阶乘的程序,c,factorial,C,Factorial,当我运行这个程序时,它也会给出答案,但当选择选项n时,它会再次要求输入选项检查它,并提供很好的解决方案 #include<stdio.h> int main() { char ch; static int count = 0; static int num; static unsigned long long int res; static int temp; if (count == 0) { printf(

当我运行这个程序时,它也会给出答案,但当选择选项n时,它会再次要求输入选项检查它,并提供很好的解决方案

#include<stdio.h>
int main()
{
    char ch;
    static int count = 0;
    static int num;
    static unsigned long long int res;
    static int temp;

    if (count == 0)
    {
        printf("Enter the value of Number:");
        scanf("%d", &num);
        if (num < 0)
        {
            printf("Invalid input.");
            return 0;
        }
        temp = num;
        res = 1;
        count++;

        do {
            main();
            printf("Factorial of %d is\t:%llu\n", temp, res);
            printf("Do you want to continue y/n :");
            scanf("\n%c", &ch);

        } while (ch == 'y' || ch == 'Y');
    }

    //Logic for recursive factorial function
    if (num > 0)
    {
        res = res * num;
        num = num - 1;
        main();
    }
    else
    {
        count = 0;
    }
    return 1;
}
#包括
int main()
{
char ch;
静态整数计数=0;
静态整数;
静态无符号长整型;
静态温度;
如果(计数=0)
{
printf(“输入数字:”)的值;
scanf(“%d”和&num);
if(num<0)
{
printf(“无效输入”);
返回0;
}
温度=数值;
res=1;
计数++;
做{
main();
printf(“%d的阶乘是\t:%llu\n”,温度,分辨率);
printf(“是否继续?”);
scanf(“\n%c”,&ch);
}而(ch='y'| ch='y');
}
//递归阶乘函数的逻辑
如果(数值>0)
{
res=res*num;
num=num-1;
main();
}
其他的
{
计数=0;
}
返回1;
}

它再次发出请求,因为您正在递归调用do中的主函数,而。 不确定您想用这个递归调用做什么,但是非常确定您应该删除这一行


=>删除“main();”

它会再次发出请求,因为您正在递归地调用do中的main函数。 不确定您想用这个递归调用做什么,但是非常确定您应该删除这一行


=>删除“main();”

main
的递归调用是绝对的“禁止”。永远不要那样做。相反,只需在收到输入后计算阶乘

我不认为这是一个好的解决方案,但下面的代码与您自己的代码非常接近,避免了对
main
的递归调用

#include<stdio.h>
int main()
{
    char ch;
    int num;
    unsigned long long int res;
    int temp;

    do
    {
        printf("Enter the value of Number:");
        if (scanf("%d", &num) != 1 || (num < 0))
        {
            printf("Invalid input.");
            return 0;
        }

        // Calculate
        temp = num;
        res = 1;
        while (num)
        {
          res = res * num;
          num = num - 1;
        }

        printf("Factorial of %d is\t:%llu\n", temp, res);
        printf("Do you want to continue y/n :");
        scanf("\n%c", &ch);

    } while (ch == 'y' || ch == 'Y');

    return 0;
}
#包括
int main()
{
char ch;
int-num;
无符号长整型;
内部温度;
做
{
printf(“输入数字:”)的值;
如果(scanf(“%d”,&num)!=1 | |(num<0))
{
printf(“无效输入”);
返回0;
}
//算计
温度=数值;
res=1;
while(num)
{
res=res*num;
num=num-1;
}
printf(“%d的阶乘是\t:%llu\n”,温度,分辨率);
printf(“是否继续?”);
scanf(“\n%c”,&ch);
}而(ch='y'| ch='y');
返回0;
}

main
的递归调用是绝对的“禁止”。永远不要那样做。相反,只需在收到输入后计算阶乘

我不认为这是一个好的解决方案,但下面的代码与您自己的代码非常接近,避免了对
main
的递归调用

#include<stdio.h>
int main()
{
    char ch;
    int num;
    unsigned long long int res;
    int temp;

    do
    {
        printf("Enter the value of Number:");
        if (scanf("%d", &num) != 1 || (num < 0))
        {
            printf("Invalid input.");
            return 0;
        }

        // Calculate
        temp = num;
        res = 1;
        while (num)
        {
          res = res * num;
          num = num - 1;
        }

        printf("Factorial of %d is\t:%llu\n", temp, res);
        printf("Do you want to continue y/n :");
        scanf("\n%c", &ch);

    } while (ch == 'y' || ch == 'Y');

    return 0;
}
#包括
int main()
{
char ch;
int-num;
无符号长整型;
内部温度;
做
{
printf(“输入数字:”)的值;
如果(scanf(“%d”,&num)!=1 | |(num<0))
{
printf(“无效输入”);
返回0;
}
//算计
温度=数值;
res=1;
while(num)
{
res=res*num;
num=num-1;
}
printf(“%d的阶乘是\t:%llu\n”,温度,分辨率);
printf(“是否继续?”);
scanf(“\n%c”,&ch);
}而(ch='y'| ch='y');
返回0;
}

递归调用“main()”是否代表程序复位? 如果是这样,为什么不使用无限循环检查输入,然后在体内进行阶乘计算、输出(如打印等),并在不再需要某个输入时结束程序。 我相信这应该可以做到:

        do {
        printf("Do you want to find a factorial? Y || y to continue");
        scanf("%c", &ch);
        printf("Enter the value of Number:");
        scanf("%d", &num);

//Here you do your factorial number computation and error checks

    } while (ch == 'y' || ch == 'Y');

“main()”的递归调用是否代表程序的重置? 如果是这样,为什么不使用无限循环检查输入,然后在体内进行阶乘计算、输出(如打印等),并在不再需要某个输入时结束程序。 我相信这应该可以做到:

        do {
        printf("Do you want to find a factorial? Y || y to continue");
        scanf("%c", &ch);
        printf("Enter the value of Number:");
        scanf("%d", &num);

//Here you do your factorial number computation and error checks

    } while (ch == 'y' || ch == 'Y');

“仅使用主函数”和“良好解决方案”是互斥的。
if(num>0)
应更改为
while(num>1)
并取消递归。以及输入循环中的递归。整个程序需要重新构造。避免递归调用
main()
。所有你想做的事情——包括计算输入值的阶乘——都可以使用循环结构来完成。好的,但我想要的解决方案是使用递归,并且只在主函数中使用。如何做到这一点,“只使用主函数”和“好的解决方案”是互斥的。
if(num>0)
应该改为
while(num>1)
并取消递归。以及输入循环中的递归。整个程序需要重新构造。避免递归调用
main()
。所有你想做的事情——包括计算输入值的阶乘——都可以使用循环结构来完成。好的,但我想要的解决方案是使用递归,并且只在主函数中使用。如何做到这一点我必须使用递归,只调用主函数我必须使用递归,只调用主函数factorial程序使用递归,但只调用main函数,并使用选项如何继续it@piyushkumar是的,但是在if中对main的递归调用是可以的。问题是在做的时候。您应该使用递归删除itWant factorial程序,但只能通过调用main函数和do You want to continue选项来删除它it@piyushkumar是的,但是在if中对main的递归调用是可以的。问题是在做的时候。你应该移除它