saowu's Blog

C语言程序集(1-15)

C语言程序集(1-15)
2020-04-03 · 9 min read
C 日记

1.输入两个数按从小到大的顺序排列

#include <stdio.h>
int main() {
    int a, b, t;
    printf("input a,b: ");
    scanf("%d,%d", &a, &b);
    if (a > b) {
        t = a;
        a = b;
        b = t;
    }
    printf("output: %d,%d", a, b);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
input a,b: 2,1
output: 1,2
Process finished with exit code 0

2.输入3个整数把最大的输出

#include <stdio.h>
int main() {
    int a, b, c, max;
    printf("input a,b,c: ");
    scanf("%d,%d,%d", &a, &b, &c);
    max = a;
    if (max < b)
        max = b;
    if (max < c)
        max = c;
    printf("max=%d", max);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
input a,b,c: 1,2,3
max=3
Process finished with exit code 0

3.输入3个整数按从小到大的顺序打印

#include <stdio.h>
int main() {
    int a, b, c, t;
    printf("input a,b,c: ");
    scanf("%d,%d,%d", &a, &b, &c);
    if (a > b) {
        t = a;
        a = b;
        b = t;
    }
    if (a > c) {
        t = a;
        a = c;
        c = t;
    }
    if (b > c) {
        t = b;
        b = c;
        c = t;
    }
    printf("%d,%d,%d\n", a, b, c);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
input a,b,c: 2,1,3
1,2,3

Process finished with exit code 0

4.判断是否为闰年

#include <stdio.h>

int main() {

    int year;
    printf("input a year: ");
    scanf("%d", &year);
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        printf("%d is a leap year\n", year);
    else
        printf("%d is a not leap year\n", year);

    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
input a year: 2018
2018 is a not leap year

Process finished with exit code 0

5.输入一个字符,如果是大写字母,则转换为小写,否则不转换并输出最后得到的字符


#include <stdio.h>
int main() {
    char letter;
    printf("input leeter: ");
    scanf("%c", &letter);
    if ('A' <= letter && letter <= 'Z')
        letter = letter + 32;
    printf("letter: %c", letter);
    
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
input leeter:A
letter: a
Process finished with exit code 0

6.编程求解:

                /-1 (x<0)
             y=|  0 (x=0)
                \ 1 (x>0)
    
1).    
    #include <stdio.h>
    int main() {
        int x, y;
        printf("input x: ");
        scanf("%d", &x);
        if (x < 0)
            y = -1;
        else if (x == 0)
            y = 0;
        else
            y = 1;
        printf("y=%d", y);
        return 0;
    }

2).
    #include <stdio.h>
    int main() {
        int x, y;
        printf("input x: ");
        scanf("%d", &x);
        y = -1;
        if (x != 0)
            if (x > 0)
                y = 1;
            else
                y = 0;
        printf("y=%d", y);
        return 0;
    }

3).
    #include <stdio.h>
    int main() {
        int x, y;
        printf("input x: ");
        scanf("%d", &x);
        if (x >= 0)
            if (x > 0)
                y = 1;
            else
                y = 0;
        else
            y = -1;
        printf("y=%d", y);
        return 0;
    }
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
input x: 12
y=1
Process finished with exit code 0

    

7.按成绩分档次(A,B,C,D,E)

1).
    #include <stdio.h>
    int main() {
        int score;
        char grade;
        printf("input score: ");
        scanf("%d", &score);
        if (score >= 90)
            grade = 'A';
        else if (score >= 80)
            grade = 'B';
        else if (score >= 70)
            grade = 'C';
        else if (score >= 60)
            grade = 'D';
        else
            grade = 'E';
        putchar(grade);
        return 0;
    }
2).
    #include <stdio.h>
    int main() {
        int score;
        char grade;
        printf("input score: ");
        scanf("%d", &score);
        switch (score / 10) {
            case 10:
            case 9:
                grade = 'A';
                break;
            case 8:
                grade = 'B';
                break;
            case 7:
                grade = 'C';
                break;
            case 6:
                grade = 'D';
                break;
            default:
                grade = 'E';
                break;
        }
        putchar(grade);
        return 0;
    }
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
input score: 97
A
Process finished with exit code 0

8. 编程计算1+2+3+...+100=?

1).
    #include <stdio.h>
    int main() {
        int sum = 0, i = 1;
        while (i <= 100) {
            sum = sum + i;
            i++;
        }
        printf("sum=%d\n", sum);
        return 0;
    }
2).
    #include <stdio.h>
    int main() {
        int sum = 0, i = 1;
        do {
            sum=sum+i;
            i++;
        } while (i <= 100);
        printf("sum=%d\n", sum);
        return 0;
    }
3).
    #include <stdio.h>
    int main() {
        int sum = 0;
        for (int i = 1; i <= 100; i++)
            sum += i;
        printf("sum=%d\n", sum);
        return 0;
    }
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=5050

Process finished with exit code 0

9.计算2+4+6+...+100=?


#include <stdio.h>
int main() {
    int sum = 0;
    for (int i = 100; i >= 1; i -= 2)
        sum += i;
    printf("sum=%d\n", sum);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=2550

Process finished with exit code 0

10.计算1+3+5+...+99=?


#include <stdio.h>
int main() {
    int sum = 0;
    for (int i = 1; i <= 100; i += 2)
        sum += i;
    printf("sum=%d\n", sum);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=2500

Process finished with exit code 0

11.计算123*...*10=?

1).
    #include <stdio.h>
    int main() {
        int fact = 1, k = 1;
        while (k <= 10) {
            fact = fact * k;
            k++;
        }
        printf("fact=%d\n", fact);
        return 0;
    }
2).
    #include <stdio.h>
    int main() {
        int fact = 1;
        for (int k = 1; k <= 10; k++) {
            fact *= k;
        }
        printf("fact=%d\n", fact);
        return 0;
    }
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
fact=3628800

Process finished with exit code 0

12.1~100之间所有自然数平方和


#include <stdio.h>
int main() {
    int sum = 0;
    for (int i = 1; i <= 100; i++)
        sum = sum + i * i;
    printf("sum=%d\n", sum);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=338350

Process finished with exit code 0

13.1~100之间能被3整除的数之和


#include <stdio.h>
int main() {
    int sum = 0;
    for (int i = 1; i <= 100; i++)
        if (i % 3 == 0)
            sum += i;
    printf("sum=%d\n", sum);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=1683

Process finished with exit code 0

14.从键盘上输入一串字符,分别将字母和数字以及其他字符做统计并输出


#include <stdio.h>
int main() {
    int letter = 0, digit = 0, other = 0;
    char ch;
    while ((ch = getchar()) != '!') {
        if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
            letter++;
        else if (ch >= '0' && ch <= '9')
            digit++;
        else
            other++;
    }
    printf("letter:%d\ndigit:%d\nother:%d\n", letter, digit, other);
    return 0;
}

/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sdhjashjdhasjh3112312JDHSAJDKJSDJ!
letter:26
digit:7
other:0

Process finished with exit code 0

15.从键盘上输入一串字符,要求判断每一个字符,大写字母转成小写字母,小写字母转换成大写字母,其他不变并输出


#include <stdio.h>
int main() {
    char ch;
    while ((ch = getchar()) != '!') {
        if (ch >= 97 && ch <= 122)
            ch -= 32;
        else if (ch >= 65 && ch <= 90)
            ch += 32;
        putchar(ch);
    }
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sdjkasdjkasjjdkdJSDAKDJAKSJ2321312!
SDJKASDJKASJJDKDjsdakdjaksj2321312
Process finished with exit code 0

Copyright © 2020 - 2024 saowu. All Right Reserved
Powered by Gridea