saowu's Blog

C语言程序集(16-30)

C语言程序集(16-30)
2020-04-03 · 7 min read
C 日记

16.打印*的10*10矩阵


#include <stdio.h>
int main() {
    for (int i = 0; i < 10; i++){
        for (int j = 0; j < 10; j++)
            putchar('*');
        putchar('\n');
    }
    return 0;
}

输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

Process finished with exit code 0

17.打印:

        *
        **
        ***
        ****
        *****

#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= i; j++)
            putchar('*');
        putchar('\n');
    }
    return 0;
}

18.打印:

        *****
        ****
        ***
        **
        *
        
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        for (int j = 5; j >= i; j--)
            putchar('*');
        putchar('\n');
    }
    return 0;
}


19.打印:

            *
            ***
            *****
            *******
            *********
            
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= 2 * i - 1; j++)
            putchar('*');
        putchar('\n');
    }
    return 0;
}

20.打印:

            *
           ***
          *****
         *******
        *********
        
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= 5 - i; j++)
            putchar(' ');
        for (int j = 1; j <= 2 * i - 1; j++)
            putchar('*');
        putchar('\n');
    }
    return 0;
}

21.打印:

        *********
         *******
          *****
           ***
            *
            
#include <stdio.h>
int main() {
    for (int i = 5; i >= 1; i--) {
        for (int j = 1; j <= 5 - i; j++)
            putchar(' ');
        for (int j = 1; j <= 2 * i - 1; j++)
            putchar('*');
        putchar('\n');
    }
    return 0;
}

22.打印:

        *********
         *******
          *****
           ***
            *
           ***
          *****
         *******
        *********
        
#include <stdio.h>
int main() {
    for (int i = 5; i >= 1; i--) {
        for (int j = 1; j <= 5 - i; j++)
            putchar(' ');
        for (int j = 1; j <= 2 * i - 1; j++)
            putchar('*');
        putchar('\n');
    }
    for (int i = 2; i <= 5; i++) {
        for (int j = 1; j <= 5 - i; j++)
            putchar(' ');
        for (int j = 1; j <= 2 * i - 1; j++)
            putchar('*');
        putchar('\n');
    }
    return 0;
}

23.打印:

            *
           ***
          *****
         *******
        *********
         *******
          *****
           ***
            *
            
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= 5 - i; j++)
            putchar(' ');
        for (int j = 1; j <= 2 * i - 1; j++)
            putchar('*');
        putchar('\n');
    }
    for (int i = 4; i >= 1; i--) {
        for (int j = 1; j <= 5 - i; j++)
            putchar(' ');
        for (int j = 1; j <= 2 * i - 1; j++)
            putchar('*');
        putchar('\n');
    }

    return 0;
}

24.打印乘法口诀表


#include <stdio.h>
int main() {
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= i; j++)
            printf("\t%d*%d=%d", i, j, i * j);
        printf("\n");
    }
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
	1*1=1
	2*1=2	2*2=4
	3*1=3	3*2=6	3*3=9
	4*1=4	4*2=8	4*3=12	4*4=16
	5*1=5	5*2=10	5*3=15	5*4=20	5*5=25
	6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36
	7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49
	8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64
	9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81

Process finished with exit code 0

25.打印:

           1   2   3   4
           5   6   7   8
           9  10  11  12
          13  14  15  16
          
#include <stdio.h>
int main() {
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 4; j++)
            printf("%4d", (i - 1) * 4 + j);
        printf("\n");
    }
    return 0;
}

26.打印:

           1   5   9  13
           2   6  10  14
           3   7  11  15
           4   8  12  16
           
#include <stdio.h>
int main() {
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 4; j++)
            printf("%4d", (j - 1) * 4 + i);
        printf("\n");
    }
    return 0;
}

27.(真题)打印:

          16  15  14  13
          12  11  10   9
           8   7   6   5
           4   3   2   1
           
#include <stdio.h>
int main() {
    for (int i = 4; i >= 1; i--) {
        for (int j = 1; j <= 4; j++)
            printf("%4d", i * 4 - (j - 1));
        printf("\n");
    }
    return 0;
}

28.素数的判断

1).
    #include <stdio.h>
    #include <math.h>
    
    int main() {
        int i, x;
        printf("input x :");
        scanf("%d", &x);
        for (i = 2; i <= sqrt(x); i++)
            if (x % i == 0)
                break;
        if (i > sqrt(x))
            printf("%d is a prime number.\n", x);
        else
            printf("%d is not a prime number.\n", x);
        return 0;
    }
2).
    #include <stdio.h>
    #include <math.h>
    int main() {
        int i, x, flag = 1;
        printf("input x :");
        scanf("%d", &x);
        for (i = 2; i <= sqrt(x); i++)
            if (x % i == 0) {
                flag = 0;
                break;
            }
        if (flag == 1)
            printf("%d is a prime number.\n", x);
        else
            printf("%d is not a prime number.\n", x);
        return 0;
    }

29.打印100~200之间的素数


#include <stdio.h>
#include <math.h>

int main() {
    int flag;
    for (int x = 100; x <= 200; x++) {
    //for(int x = 101; x <=199; x += 2){
        flag = 1;
        for (int i = 2; i <= sqrt(x); i++)
            if (x % i == 0) {
                flag = 0;
                break;
            }
        if (flag == 1)
            printf("%d\t", x);
    }

    return 0;
}

30.打印ASCII码与字母


#include <stdio.h>

int main() {
    for (int ch = 'a'; ch <= 'z'; ch++) {
        printf("%c=%d %c=%d ", ch - 32, ch - 32, ch, ch);
    }
    return 0;
}
Copyright © 2020 - 2024 saowu. All Right Reserved
Powered by Gridea