C语言 在 c 中打印 N 个第一个阶乘数的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22111065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:50:18  来源:igfitidea点击:

Program to print N first factorial numbers in c

cloopsfor-loopfactorial

提问by user3120705

Factorial number is a number that is multiplied by it's previous numbers. For example it's 5. 1*2*3*4*5 is it's factorial number. I already made a program which prints factorial of any number, but i don't know how to make it to print N first factorial number in c. For example i type 10. It must show first 10 numbers along with their factorials (Making a table) Here is what i was made to print factorial of any number.Is there any possibility to do with while/ if else statements/ and for loop?

阶乘数是一个数乘以其先前的数。例如它是 5。1*2*3*4*5 是它的阶乘数。我已经制作了一个打印任何数字阶乘的程序,但我不知道如何让它在 c 中打印 N 个第一个阶乘数。例如,我输入 10。它必须显示前 10 个数字及其阶乘(制作表格)这是我用来打印任何数字的阶乘的内容。是否有可能与 while/if else 语句/和 for 循环有关?

#include <stdio.h>
int main()
{
    int i, n, fakt = 1;
    printf("Enter a number:\n");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
         fakt = fakt*i;
    printf("Factorial of %d js %d\n", n, fakt);
    getch();
}

回答by Rikayan Bandyopadhyay

You probably want this:

你可能想要这个:

Program:

程序:

#include <stdio.h>
int main()
{
    int i, n, fakt = 1;
    printf("Enter a number:\n");
    scanf("%d", &n);
    for (i=1;i<= n;i++) //use braces to write more than one statement inside the loop
    {
         fakt=fakt*i;
         printf("Factorial of %d is %d\n", i, fakt);
    }
    getch();
}

Output:

输出:

Enter a number:
5
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120

回答by droidman

#include <conio.h>
#include <stdio.h>

void main()
{
    int f=1,i,v;

    clrscr();
    printf("Enter the number :");
    scanf("%d",&v);
        for(i=1;i<=v;i++)
        {
            f=f*i;
            printf("num =%d and fac=%d\n",i,f);
        }
    getch();
}

this code will work

此代码将工作

回答by Dan D.

That code already uses the forloop. The whileloop equivalent is:

该代码已经使用了for循环。while等价的循环是:

i = 1;
while (i <= n) {
    fakt = fakt*i;
    i++;
}

回答by reza.safiyat

You can do a nested loop.

您可以执行嵌套循环。

Run the parent loop from 1 to n, and the nested loop will be your already working for loop.

从 1 到 n 运行父循环,嵌套循环将是您已经在工作的 for 循环。

You may want this:

你可能想要这个:

    #include <stdio.h>

    int main()
    {
            int n, i, num, factorial;
            printf("Enter the number of terms: ");
            scanf("%d", &n);

            for(i = 1; i <= n; i++)
            {
                    num = i;
                    factorial = 1;
                    while(num)
                            factorial *= num--;
                    printf("%d \t %d\n", i, factorial);
            }
            return 0;
    }

Output:

输出:

    Enter the number of terms: 10
    1    1
    2    2
    3    6
    4    24
    5    120
    6    720
    7    5040
    8    40320
    9    362880
    10   3628800

回答by user3203860

#include <stdio.h>
int factorial(int n)
{
    int i,fakt = 1;
    printf("Enter a number:\n");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
       fakt = fakt*i;
    return fakt;
}

int main()
{
   int n;
   printf("Enter a number:\n");
   scanf("%d", &n);
   int i = 0;
   for(i=1;i<=n;i++)
   {
       printf("Factorial for %d is %d\n",i,factorial(i));
   }
   return 0;
}

I think this will do the job just fine.

我认为这会很好地完成工作。

回答by Kalanidhi

Use this fastest version of factorial using recursion with if ..else statement

使用带有 if ..else 语句的递归使用这个最快的阶乘版本

#include<stdio.h>
int fact(int n);
void main()
{
    int n;
    printf("\nEnter an integer:");
    scanf("%d",&n);
    fact(n);
}
int fact(int n)
{
    int a;
    if(n==0)
    {
            printf("The Factorial of 0 is 1\n");                                                                                                   
            return 1;
    }
    else
    {
            a=n*fact(n-1);
            printf("The Factorial of %d is %d\n",n,a);
            return a;
    }
}

回答by Goutam Chakraborty

    #include<stdio.h>
    int main(int n){
    int fact;
    clrscr();
    printf("Enter a number and type exit:\n");
    scanf("%d",&n);
    if(n!=0){
    fact=n*main(n-1);
    printf("Factorial of %d is %d\n",n,fact);
    getch();
    return fact;
    }
    else{
    printf("Factorial of 0 is 1.\n");
    getch();
    return 1;
    }
    }