在C语言程序中计算数字阶乘的终极指南

207 阅读3分钟

在C语言程序中寻找数字阶乘的终极指南

C Program to Find the Factorial of a Number

目录

一个正整数(数字)的阶乘是小于该正整数的所有整数的乘法之和。例如,5的阶乘是5*4*3*2*1,等于120。

阶乘用"!"来表示。因此,假设你想找到数字n的阶乘,那么n!=n*(n-1)*(n-2)*(n-3)...*。

现在,让我们来看看如何写一个C程序来求一个数字的阶乘?

阶乘的C语言程序的算法

找出一个数字的阶乘的C语言程序的算法是:

  • 启动程序
  • 要求用户输入一个整数来求阶乘
  • 读取整数并将其分配给一个变量
  • 从整数的值到1,将每个数字相乘并更新最终值
  • 在所有的乘法运算结束后,直到1的最终值就是阶乘。
  • 程序结束

阶乘的C语言程序伪码

使用上述算法,我们可以创建C语言程序的伪代码,以寻找一个数字的阶乘,例如:

procedure fact(num)

until num=1

fact = fact*(num-1)

Print fact

end procedure

现在我们知道了写阶乘C程序的基本算法和伪代码,让我们开始用各种方法实现它。

现在,让我们开始使用各种方法来实现它。

使用for循环查找阶乘的C语言程序

我们将首先使用for循环来编写一个C程序,以求得一个数字的阶乘。该程序将有一个值为1的整数变量,for循环将在每次迭代中不断增加该值,直到该数字等于用户输入的数字。事实变量中的最终值将是用户输入的数字的阶乘。

例子:

#include<stdio.h>

int main(){

    int x,fact=1,n;

    printf("Enter a number to find factorial: ");

    scanf("%d",&n); 

    for(x=1;x<=n;x++)

        fact=fact*x; 

    printf("Factorial of %d is: %d",n,fact);

    return 0;

}

使用while循环查找阶乘的C程序

在这个例子中,我们将使用while循环来实现这个算法,并找到一个数字的阶乘。

#include<stdio.h>

int main(){

    int x=1,fact=1,n;

    printf("Enter a number to find factorial: ");

    scanf("%d",&n);

    while(x<=n){

        fact=fact*x;

        x++;

    } 

    printf("Factorial of %d is: %d",n,fact);

    return 0;

}

使用递归查找阶乘的C语言程序

现在,我们将使用一个递归函数编写一个阶乘程序。递归函数将调用自己,直到数值不等于0。

现在,我们将使用递归函数编写一个阶乘的C程序。递归函数将自我调用,直到数值不等于0。

例子:

#include <stdio.h>

int main(){

int x = 7;

printf("The factorial of the number is %d", fact(x));

return 0;

}

// Recursive function to find factorial

int fact(int y){

if (y == 0)

return 1;

return y * fact(y - 1);

}

使用三元运算符查找阶乘的C语言程序

三元运算符就像if...else语句的简写。它提供了两个条件和根据条件执行的语句。下面是使用三元运算符的阶乘的C程序。

下面是使用三元运算符的阶乘程序。

#include <stdio.h>

int main(){

int n = 4;

printf("The factorial of %d is %d",

n, fact(n));

return 0;

}

int fact(int x){

// Using ternary operator

return (x == 1 || x == 0)

? 1

: x * fact(x - 1);

}

使用tgamma()方法查找阶乘的C程序

tgamma()函数是C语言编程中math.h库中定义的一个库函数。它计算所传递参数的伽玛函数。

让我们用这个函数来编写一个C阶乘程序。

#include <math.h>

#include <stdio.h>

int main(){

int x = 5;

x = tgamma(x + 1);

printf("%d", x);

return 0;

}

注意:tgamma()函数只对小整数有效,因为C语言不能存储大数值。另外,对于5以上的整数,你必须在最后的输出中加1来得到阶乘。

使用函数查找阶乘的C语言程序

你也可以创建自己的函数来寻找一个给定数字的阶乘。下面的代码演示了使用函数求阶乘的方法。

例子:

#include<stdio.h>

int findFact(int);

int main(){

    int x,fact,n;

    printf("Enter a number to get factorial: ");

    scanf("%d",&n);

    fact = findFact(n);

    printf("The factorial of %d is: %d",n,fact);

    return 0;

}

int findFact(int n){

    int x,fact=1;

    for(x=1;x<=n;x++)

      fact=fact*x;

     return fact;

}

使用指针查找阶乘的C语言程序

在这个例子中,我们将创建一个C语言程序,使用C语言指针寻找一个数字的阶乘。下面是使用指针查找阶乘的代码。

#include<stdio.h>

void findFact(int,int *);

int main(){

    int x,fact,n;

    printf("Enter a number to get factorial: ");

    scanf("%d",&n); 

    findFact(n,&fact);

    printf("The factorial of %d is: %d",n,fact); 

    return 0;

} 

void findFact(int n,int *fact){

    int x;

    *fact =1;

    for(x=1;x<=n;x++)

        *fact=*fact*x;

}

查找阶乘系列的C语言程序

除了寻找单个数字的阶乘,我们还可以写一个C程序来寻找一个系列中多个数字的阶乘。下面的代码演示了如何找到一个范围内的数字的阶乘系列。

#include<stdio.h>

int main(){

    long fact=1;

    int x,n,s_range,e_range; 

    printf("Enter the starting range: ");

    scanf("%d",&s_range); 

    printf("Enter the ending range: ");

    scanf("%d",&e_range); 

    printf("Factorial series of the given range is: ");

    for(n=s_range;n<=e_range;n++){

        fact=1; 

        for(x=1;x<=n;x++){

            fact=fact*x;

        } 

        printf("%ld ",fact);

    }

    return 0;

}

总结

在这篇文章中,你已经学会了如何编写C语言程序,以不同的方式找到一个数字的阶乘。