????????在 C 語言學習過程中,通過實戰編程題鞏固知識點是非常有效的方式。本文整理了一系列經典 C 語言編程題,涵蓋基礎計算、邏輯判斷、圖形打印等多個維度,并附上完整代碼與解析,適合初學者參考學習
上機題
1.
計算n以內所有正奇數的和 ? n值通過鍵盤輸入
代碼:
/*************************************************************************> File Name: demo04.c> Author: 千夕> Description: > Created Time: 2025年07月16日 星期三 15時19分09秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{int num = 0;int i = 1;int n;printf("請輸入數字:");scanf("%d",&n);while(i < n){if(i % 2 != 0 )num += i; i++;}printf(" %d 以內所有正奇數和為: %d\n",n,num);return 0;
}
運行結果:
2.
計算 1 + 1/(2 * 3) + 1/(3 * 4) + …+ 1/(n * (n + 1)) = ?直到最后一相值小于0.00001為 止
代碼:
/*************************************************************************> File Name: demo05.c> Author: qianxi> Description: > Created Time: 2025年07月16日 星期三 18時15分43秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{float i = 2;float sum=0;float n=0;for(;1 / (i * (i + 1)) > 0.00001; i++){n = 1 / (i * (i + 1));sum += n;}printf("這個式子的答案是:%f\n",sum+1);return 0;
}
運行結果:
3.
計算1+1/2 - 1/3 + 1/4 - 1/5 …+1/n= ? n通過鍵盤輸入(int k = -1)
代碼:
/*************************************************************************> File Name: demo06.c> Author: 千夕> Description: > Created Time: 2025年07月16日 星期三 19時02分32秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{int i = 1;float sum1,sum2;int x;float sum = 0;printf("請輸入正整數:");/*if(scanf("%d",&x) != 1 || x <= 0){printf("請輸入一個正整數。\n");return 1;}
*/scanf("%d",&x);while(i <= x){if(i % 2 == 0){sum1 +=1.0 / i;}else {sum2 += 1.0 / i;}i++;}sum = sum1-sum2+2;printf("這個式子的值為:%.6f\n",sum);return 0;
}
運行結果:
4.
計算n的階乘 ? n! = 123…*n n值通過鍵盤輸入
代碼:
/*************************************************************************> File Name: demo07.c> Author: 千夕> Description: > Created Time: 2025年07月16日 星期三 21時40分18秒************************************************************************/#include <stdio.h>
#include <math.h>int main(int argc,char *argv[])
{unsigned long n;size_t r = 1;printf("請輸入一個整數:");scanf("%lu",&n);for(int i = 1; i <= fabs(n); i++) r *=i;printf("1~%lu的階乘為%lu\n",(size_t)fabs(n),r);return 0;
}
運行結果:
5.
輸出半徑為1~10的圓面積,面積大于100時停止
代碼:
/*************************************************************************> File Name: demo10.c> Author: 千夕> Description: > Created Time: 2025年07月16日 星期三 23時53分40秒************************************************************************/#include <stdio.h>
#define PI 3.1415926int main(int argc,char *argv[])
{double area;for (int r = 1; r <= 10;r++){area = PI * r * r;if (area > 100){break;}printf("半徑為%d的圓的面積為:%lf\n",r,area);}return 0;
}
運行結果:
6
求輸入的十個整數中正數的個數及其平均值
代碼:
/*************************************************************************> File Name: demo08.c> Author: 千夕> Description: > Created Time: 2025年07月16日 星期三 22時34分46秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{int a;int b = 0;double sum = 0;printf("請輸入十個數字:");for(int i = 1; i <= 10;i++){scanf("%d",&a);if(a > 0){b++;sum +=a;}}printf("10個數中的正整數個數為%d個,平均值為%.2lf\n",b,sum/b);return 0;
}
運行結果:
7
打印出100以內能整除7之外的的自然數
代碼:
/*************************************************************************> File Name: demo09.c> Author: 千夕> Description: > Created Time: 2025年07月16日 星期三 23時19分29秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{printf("100以內不能整除7的數為:");for(int i = 1; i <= 100; i++){if(i % 7 != 0){printf("%d ",i);}}printf("\n");return 0;
}
運行結果:
8
打印乘法表
代碼:
/*************************************************************************> File Name: demo11.c> Author: 千夕> Description: > Created Time: 2025年07月16日 星期三 23時59分57秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{printf("=======九九乘法表=======\n");for(int i = 1;i <=9 ;i++){for( int j = 1; j <= i;j++){printf("%d×%d=%d\t",j,i,j*i);}printf("\n");}printf("\n");return 0;
}
運行結果:
9
我國古代數學家張丘建在《算經》一書中提出的數學問題:雞翁一值錢五,雞母一值 錢三,雞雛三值錢一。百錢買百雞,問雞翁、雞母、雞雛各幾何?
代碼:
/*************************************************************************> File Name: test5.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 19時46分44秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{printf("百錢買百雞問題\n");printf("-----------------\n");for(int x = 0;x <= 100; x++){for(int y = 0; y <= 100 - x;y++){for(int z = 0;z <= 100 - x - y;z++){if(x + y + z == 100 && 5 * x + 3 * y + z / 3 == 100)printf("%d\t%d\t%d\n",x,y,z);}}}return 0;
}
運行結果:
10
從鍵盤上輸入多個無符號整型數據,直到 0 結束 ,輸出所輸入數據中的最大值。
代碼:
/*************************************************************************> File Name: test2.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 09時59分09秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{unsigned num,max = 0;while(1){scanf("%u",&num);if(num == 0){break;}else if(num > max){max = num;}}printf("最大值是:%u\n",max);return 0;
}
運行結果:
思考題
1
判斷一個數是不是回文數。(回文數是指正序(從左向右)和倒序(從右向左)讀都是 一樣的整數。如:12321
代碼:
/*************************************************************************> File Name: test6.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 20時02分53秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{int num,originalNum,n = 0,x;printf("請輸入數字:");scanf("%d",&num);originalNum = num;for (;num != 0;){x = num % 10;n = n * 10 + x;num /= 10;}num = originalNum;if(originalNum == n)printf("這串數字是回文數\n");else printf("這串數字不是回文數\n");return 0;
}
運行結果:
2
通過鍵盤錄入一個整數,判斷這個整數是否是水仙花數。
代碼:
/*************************************************************************> File Name: test4.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 19時22分30秒************************************************************************/#include <stdio.h>
#include <math.h>
int main(int argc,char *argv[])
{int num, originalNum,n=0,sum=0;printf("請輸入整數:");scanf("%d",&num);originalNum = num;while(num != 0){num /= 10;n++;}num = originalNum;while(num != 0){int digit = num % 10;sum += pow(digit,n);num /= 10;}if (sum == originalNum)printf("%d是水仙花數\n",originalNum);elseprintf("%d不是水仙花數\n",originalNum);return 0;
}
運行結果:
擴展題
使用 * 在控制臺分別打印上直角三角形、下直角三角形,上等腰三角形、下等腰三角 形、菱形、平行四邊形。
①上直角三角形代碼:
/*************************************************************************> File Name: demo7.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 20時55分11秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{int i,j;for(i = 0;i <=5;i++){for(j = 0;j <= i;j++){printf("*");}printf("\n");}return 0;
}
運行結果:
②下直角三角形代碼:
/*************************************************************************> File Name: demo8.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 21時17分35秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{int x,y;for (x=5;x >= 0;x--){for (y = 0; y <= x ;y++)printf("*");printf("\n");}return 0;
}
運行結果:
③上等腰三角形代碼:
/*************************************************************************> File Name: demo09.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 22時06分10秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{int i,j,space;for(i = 1;i<=5;i++){for(space = 1;space <= 5 - i; space++){printf(" ");}for(j = 1;j<=2*i -1;j++){printf("*");}printf("\n");}return 0;
}
運行結果:
④下等腰三角形代碼:
/*************************************************************************> File Name: demo10.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 22時31分07秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{int i,j,space;for(i = 5;i >=1 ; i--){for(space = 1;space <= 5-i;space++){printf(" ");}for(j = 1;j <= 2*i-1;j++){printf("*");}printf("\n");}return 0;
}
運行結果:
⑤菱形代碼:
/*************************************************************************> File Name: demo11.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 22時52分29秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{ int i,j,space;for(i = 1;i<= 5;i++){for(space = 1;space<=5-i;space++){printf(" ");}for(j = 1;j<=2*i-1;j++){printf("*");}printf("\n");}for(i = 4 ;i>=1;i--){for(space = 1;space<=5-i;space++){printf(" ");}for(j = 1;j<=2*i-1;j++){printf("*");}printf("\n");}return 0;
}
運行結果:
⑥平行四邊形代碼:
/*************************************************************************> File Name: demo12.c> Author: 千夕> Description: > Created Time: 2025年07月17日 星期四 23時05分45秒************************************************************************/#include <stdio.h>int main(int argc,char *argv[])
{int i,j,space;for(i=1;i<=5;i++){for(space = 1;space<=5-i;space++)printf(" ");for(j=1; j<=5;j++){printf("*");}printf("\n");}return 0;
}
運行結果:
總結
????????本文涵蓋的編程題涉及 C 語言基礎語法(循環、條件判斷、數組等)和算法邏輯,通過這些實例可以加深對 C 語言的理解與應用。實際編程中,需注意變量初始化、邊界條件處理及數據類型選擇,避免出現邏輯錯誤或溢出問題。初學者可在此基礎上嘗試優化代碼,如增加輸入校驗、擴展功能等,提升編程能力。