1. 一维数组打印杨辉三角

    #include <stdio.h>
    int main() {
    int a[10] = {1,1};
    printf("%5d\n",a[0]);
    printf("%5d%5d\n",a[0],a[1]);
    for (int i = 2; i < 10; ++i) {
    a[i] = 1;
    for (int j = i - 1; j >= 1; --j) {
    a[j] = a[j-1] + a[j];
    }
    for (int j = 0; j <= i; ++j) {
    printf("%5d",a[j]);
    }
    printf("\n");
    }
    return 0;
    }
  2. 判断一个数是否为素数,是否对称

    #include<stdio.h>
    #include<math.h>
    int sushu(int n) {
    int res = 1;
    int i;
    for(i = 2; i <= sqrt(n); i++) {
    if(n % i == 0) {
    res = 0;
    break;
    }
    }
    return res;
    }
    int duichen(int n) {
    int res = 0;
    int sum = 0;
    int m = n;
    int k;
    do {
    k = m % 10;
    sum = sum * 10 + k;
    m = m / 10;
    } while(m);
    if(sum == n) res = 1;
    return res;
    }
    int main() {
    int n;
    printf("请输入要判断的数:\n");
    scanf("%d", &n);
    if(sushu(n)) {
    printf("%d是素数!\n", n);
    } else {
    printf("%d不是素数!\n", n);
    }
    if(duichen(n)) {
    printf("%d是对称数!\n", n);
    } else {
    printf("%d不是对称数!\n", n);
    }
    return 0;
    }
  3. 质因数分解

    #include<stdio.h>
    int main() {
    int n;
    int i;
    printf("请输入待分解的数:\n");
    scanf("%d", &n);
    printf("%d =", n);
    while(n != 1) {
    for(i = 2; i <= n; i++) {
    if(n % i == 0) {
    n = n / i;
    printf(" %d ", i);
    if(n != 1) {
    printf("*");
    } else {
    printf("\n");
    }
    break;
    }
    }
    }
    return 0;
    }
  4. 水仙花数

    #include<stdio.h>
    #include<stdlib.h>

    int shuixianhua(int n) {
    int i, sum = 0, k = n;
    int result = 0;
    do {
    i = k % 10;
    k /= 10;
    sum += i * i * i;
    } while (k);
    if (sum == n) {
    result = 1;
    }
    return result;
    }

    int main() {
    FILE * in , * out;//注意FILE全大写
    int m;
    printf("请输入要判断是否为水仙花的数:\n");
    if((in = fopen("shui.in", "w")) == NULL) {
    printf("cannot open file.\n");
    exit(0);
    }
    if((out = fopen("shui.out", "w")) == NULL) {
    printf("cannot open file.\n");
    exit(0);
    }
    while (scanf("%d", &m) != EOF) {
    fprintf(in, "%d\n", m);
    if(m == 0) {
    break;
    }
    if(shuixianhua(m)) {
    fputs("T\n", out);//等效于 fprintf(out,"T/n");
    printf("T\n");
    } else {
    fputs("F\n", out);
    printf("F\n");
    }
    }
    fclose(in);
    fclose(out);
    system("pause");
    return 0;
    }