本文目錄一覽:
用C語言編寫1到10的階乘
#includestdio.h
int main()
{
int a=1,i;
for(i=1;i=10;i++)
a=a*i;
printf(“10的階乘=%d”,a);
return 0;
}
擴展資料:
在C語言中,有三種類型的循環語句:for語句、while語句和do While語句。分別介紹如下:
for
for為當型循環語句,它很好地體現了正確表達循環結構應注意的三個問題:
⑴控制變量的初始化。
⑵循環的條件。
⑶循環控制變量的更新。
while:
while結構循環為當型循環(when type loop),一般用於不知道循環次數的情況。維持循環的是一個條件表達式,條件成立執行循環體,條件不成立退出循環。
while語句格式為:
while(條件表達式)
循環體
每次執行循環體前都要對條件表達式進行判斷。
參考資料來源:百度百科-循環語句
c語言求1到100階乘代碼
1到100?這數夠大的了…
#includestdio.h
void main()
{
double j=1;
for(int i=1;i=50;i++)j*=i;
printf(“1到100的階乘是%.0f\n”,j);
}
下面用函數的遞歸調用做:
#includestdio.h
double fact(int n)/*求階乘的函數*/
{
double j;
if(n1)j=n*fact(n-1);/*遞歸調用,當n1時,一直會調用下去,只不過每次參數被減1*/
else
return 1;/*當n被減到1時,返回1,如是會被累積,當n初始為1時直接返回1*/
return j;/*j是當n1時,最後要的結果*/
}
void main()
{
printf(“1到n的階乘是%.0f\n”,fact(5));
}
c語言考試,求編程,,???第三大題,可以給截圖,
第三大題第三問:
package HXY;
import java.util.*;
public class Array{
public static void main(String[] args) {
int englishCount=0;
int spaceCount=0;
int numCount=0;
int otherCount=0;
Scanner sc=new Scanner(System.in);
System.out.println(“請輸入一段話”);
String str=sc.nextLine();
char[] ch=str.toCharArray();
for(int i=0;ich.length;i++){
if(Character.isLetter(ch[i])){;
englishCount++;
}
else if(Character.isSpaceChar(ch[i])){
spaceCount++;
}
else if(Character.isDigit(ch[i])){
numCount++;
}
else{
otherCount++;
}
}
System.out.println(“字母個數”+englishCount);
System.out.println(“數字個數”+numCount);
System.out.println(“空格個數”+spaceCount);
System.out.println(“其他符號個數”+otherCount);
}
}
運行結果如下:
請輸入一段話
ef dfggf 1234
字母個數7
數字個數4
空格個數2
其他符號個數0
第三大題第一問:
public class Y {
public static long show(long n){
if(n==1){
System.out.println(“階乘結果是1”);
return 1;
}
else{
long num =(long) (n*show(n-1));
System.out.println(“階乘結果是:”+num);
return num;
}
}
public static void main(String[] args) {
Y y=new Y();
y.show(10);
}
}
運行結果如下:
階乘結果是1
階乘結果是:2
階乘結果是:6
階乘結果是:24
階乘結果是:120
階乘結果是:720
階乘結果是:5040
階乘結果是:40320
階乘結果是:362880
階乘結果是:3628800
這是我用java語言寫的,謝謝採納。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/232485.html