C语言编程练习题:第一期

练习题1: 编写一个程序,计算并输出两个整数之和。

复制代码
#include <stdio.h>

int main() {
    int num1, num2;
    
    printf("Enter two integers: ");
    scanf("%d %d", &num1, &num2);
    
    int sum = num1 + num2;
    printf("The sum of %d and %d is %d\n", num1, num2, sum);
    
    return 0;
}

练习题2: 编写一个程序,判断用户输入的年份是否为闰年。闰年的条件是:能被4整除但不能被100整除,或者能被400整除。

复制代码
#include <stdio.h>

int main() {
    int year;
    
    printf("Enter 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 not a leap year.\n", year);
    }
    
    return 0;
}

练习题3: 编写一个程序,计算并输出给定整数数组的所有元素之和。

复制代码
#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 0;
    
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    
    printf("The sum of all elements in the array is %d\n", sum);
    
    return 0;
}

练习题4: 编写一个程序,实现字符串反转功能。

复制代码
#include <stdio.h>
#include <string.h>

void reverse(char* str) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    
    str[strcspn(str, "\n")] = '\0'; // Remove trailing newline
    
    reverse(str);
    printf("Reversed string: %s\n", str);
    
    return 0;
}

练习题5: 编写一个程序,找出给定整数数组中的最大值和最小值。

复制代码
#include <stdio.h>

int main() {
    int arr[] = {9, 7, 8, 6, Ⅰ1, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int max = arr[0], min = arr[0];
    
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
        if (arr[i] < min) {
            min = arr[i];
        }
    }
    
    printf("The maximum value in the array is %d\n", max);
    printf("The minimum value in the array is %d\n", min);
    
    return 0;
}
相关推荐
新能源BMS佬大7 分钟前
【仿真到实战】STM32落地EKF算法实现锂电池SOC高精度估算(含硬件驱动与源码)
stm32·嵌入式硬件·算法·电池soc估计·bms电池管理系统·扩展卡尔曼滤波估计soc·野火开发板
wen__xvn10 分钟前
模拟题刷题2
算法
AI 菌10 分钟前
DeepSeek-OCR 解读
人工智能·算法·计算机视觉·大模型·ocr
历程里程碑28 分钟前
Linux 5 目录权限与粘滞位详解
linux·运维·服务器·数据结构·python·算法·tornado
yi.Ist35 分钟前
关于若干基础的几何问题
c++·学习·算法·计算几何
毅炼1 小时前
Netty 常见问题总结
java·网络·数据结构·算法·哈希算法
Anastasiozzzz1 小时前
leetcodehot100--最小栈 MinStack
java·javascript·算法
历程里程碑1 小时前
双指针2--盛水最多的容器
大数据·数据结构·算法·leetcode·elasticsearch·搜索引擎·散列表
hetao17338371 小时前
2026-01-22~23 hetao1733837 的刷题笔记
c++·笔记·算法
风筝在晴天搁浅1 小时前
hot100 230.二叉搜索树中第K小的元素
数据结构·算法