用c語言怎麼寫1到5的階乘的和,c語言1階乘 3階乘 5階乘程式怎麼寫?

時間 2021-12-20 16:34:03

1樓:娛樂小八卦啊

**如下:

#define _crt_secure_no_warnings 1

#include"stdio.h"

#include"stdlib.h"

int main()

int n = 0;

printf("請輸入一個數字: ");

scanf("%d",&n);

int i = 0;

int ret = 1;

int sum = 0;

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

ret *= i;

sum += ret;

printf("%d\n",sum);

system("pause");

return 0;

擴充套件資料

階乘計算功能關鍵演算法

利用遞迴方法求5!

用遞迴方式求出階乘的值。遞迴的方式為:

5!=4!*5

4!=3!*4

3!=2!*3

2!=1!*2

1!=1

即要求出5!,先求出4!;要求出4!,先求出3! … 以此類推。

注意:定義一個函式(或方法),用於求階乘的值。

在主函式(或主方法)中呼叫該遞迴函式(或方法),求出5的階乘,並輸出結果。

package third;

public class one {

public static void main(string args){

int n =5;            //輸入n的值

//n的階乘的值為:factorial(n);

system.out.println("5的階乘是:" + factorial(n));

public static int factorial(int n){         //階乘求解函式

if(n == 0){                             //判斷傳進來的n是否為0,若為零返回階乘為1

return 1;

return n*factorial(n-1);             //重新呼叫函式,繼續判斷n-1是否為零,

2樓:端嬡赧樂音

#include

"stdio.h"

main()

sum=sum+n;

printf("%d!=%d\n",j,n);

n=1;

}printf("%d",sum);}

3樓:匿名使用者

main()

sum=sum+t;

t=1;

}printf("%d",sum);}

4樓:匿名使用者

#include

int fact(int n)

void main()

c語言1階乘 3階乘 5階乘程式怎麼寫?

5樓:匿名使用者

# include "stdio.h"

void main()

printf("%d!=%d",n,t);

}else

printf("你輸入的不是正整數:");}

怎麼在c語言中計算1到5的階乘

6樓:此事與和尚無關

先定義一個變數sum作為結果的記錄,然後用一個迴圈 從1迴圈到5 依次乘起來。

**如下

#include

int main()

printf ("%d",sum) ;

return 0;}

7樓:匿名使用者

int 輸入數(0), 階乘(1), 起數(1);

cout << "求從1到輸入任意整的階乘:" << endl;

cin >> 輸入數;

do while (起數 <= 輸入數);

cout << 階乘 << endl;

8樓:苦寒凝

恩恩,樓上是構建函式的方法

下面是直接計算的,用一個for語句就可以了#include

main()

printf("%d",s);

return 0;

}---------------------------------我來幫團隊答一點題目

希望能得到你的採納^_^

9樓:匿名使用者

#include

inf fun(int n)

int main()

c語言用while語句求1到5階乘的和

10樓:匿名使用者

試試這個 ,如果可以希望採納

11樓:匿名使用者

long sum=0,n=1,x=1; //long型,保證資料長度夠用。int i=1;while(n<=5) //控制1·~ 5 。sum+=x; //求和}//sum為所求

急c語言階乘問題 求一到一百的階乘和 用while或w

include include include include using namespace std const int maxn 510 char re maxn struct big void read void print return this bool operator if a.s i...

C 求n的階乘,用c 語言求n的階乘

理陽波 以下為c 求階乘的四種方法,需要注意的是,各個 只是提供了求階乘的思路,以便在實際需要時再來編碼,各個程式都在1到10內測試正確。1 該程式在每次輸入n時,都會呼叫fac 來暴力計算以得到結果 2 該程式利用了陣列記錄已得到的結果,並在計算下一個結果時利用了已得到的結果。3 應該說該 實用性...

c語言分別輸出1的階乘到10的階乘,並累加階乘之和

12345額時代 1 開啟visual studio,新建一個空白頁面。2 在main函式的上方,我們寫上階乘函式的框架。3 然後定義一個變數 result 4 然後輸入if判斷語句。5 然後就可以寫下程式的關鍵語句。6 接下來就可以呼叫輸出。7 最後可以執行測試,這就完成了。 include in...