博客
关于我
第04节 C语言程序初体验
阅读量:329 次
发布时间:2019-03-03

本文共 1740 字,大约阅读时间需要 5 分钟。

课堂实例

  • 第一个C程序:输出“Hello world!”

    这个程序是C语言学习者的起点,旨在输出经典的问候语。代码结构清晰,包含标准输入输出库<stdio.h>,并使用printf函数进行输出。

    #include 
    int main() { printf("Hello world!\n"); return 0;}

    输出结果:

    Hello world!
  • 第二个C程序:求两个数的和

    该程序用于读取两个整数,计算它们的和,并输出结果。

    #include 
    int main() { int a, b, sum; scanf("%d %d", &a, &b); sum = a + b; printf("%d\n", sum);}

    输出结果:

    12 3446
  • 第三个C程序:英尺到米的转换

    该程序允许用户输入英尺值并将其转换为米。计算公式为米=英尺/3.28

    #include 
    int main() { float f, m; printf("请输入英尺值: "); scanf("%f", &f); m = f / 3.28; printf("转为米制单位: %f米\n", m);}

    输出结果:

    请输入英尺值: 12转为米制单位: 3.658537米

  • 实践项目

  • 计算长方形的周长和面积

    该程序读取长方形的长和宽,计算并输出周长和面积。

    #include 
    int main() { int 长, 宽, 周长, 面积; printf("长方形的长和宽: "); scanf("%d %d", &长, &宽); 周长 = (长 + 宽) * 2; 面积 = 长 * 宽; printf("长方形的周长为: %d\n", 周长); printf("长方形的面积为: %d\n", 面积); return 0;}

    输出结果:

    长方形的长和宽: 10 20长方形的周长为: 60长方形的面积为: 200
  • 摄氏温度转华氏温度

    该程序读取摄氏温度值,使用公式华氏温度=摄氏温度×9/5+32进行转换并输出结果。

    #include 
    int main() { float F, C; printf("请输入摄氏温度值: "); scanf("%f", &C); F = C * 9 / 5 + 32; printf("对应的华氏温度为: %.2f\n", F); return 0;}

    输出结果:

    请输入摄氏温度值: 10对应的华氏温度为: 50.00
  • 并联电阻值计算

    该程序读取两个并联电阻的值,计算并联后的总电阻值。

    #include 
    int main() { float R1, R2, R; printf("并联的两个电阻值: "); scanf("%f %f", &R1, &R2); R = 1 / (1 / R1 + 1 / R2); printf("并联后的电阻值为: %.2f\n", R); return 0;}

    输出结果:

    并联的两个电阻值: 10 20并联后的电阻值为: 6.67
  • 圆柱体表面积计算

    该程序读取圆柱的半径和高度,计算表面积并输出结果。

    #include 
    int main() { const float PI = 3.1415926f; float r, h, s; printf("请输入半径: "); scanf("%f", &r); printf("请输入高度: "); scanf("%f", &h); s = PI * r * r * 2 + 2 * PI * r * h; printf("圆柱表面积: %.2f\n", s); return 0;}

    输出结果:

    请输入半径: 10请输入高度: 20圆柱表面积: 1884.96
  • 转载地址:http://lcqm.baihongyu.com/

    你可能感兴趣的文章
    Oracle GoldenGate Director安装和配置(无图)
    查看>>
    oracle instr函数详解
    查看>>
    oracle ogg 单实例双向复制搭建(oracle-oracle)--Oracle GoldenGate
    查看>>
    oracle rac集群的东西之QQ聊天
    查看>>
    oracle scott趣事
    查看>>
    oracle script
    查看>>
    Oracle select表要带双引号的原因
    查看>>
    Oracle SOA Suit Adapter
    查看>>
    Oracle Spatial GeoRaster 金字塔栅格存储
    查看>>
    Oracle Spatial空间数据库建立
    查看>>
    UML— 活动图
    查看>>
    oracle sqlplus已停止工作,安装完成客户端后sqlplus报“段错误”
    查看>>
    oracle SQLserver 函数
    查看>>
    Oracle Statspack分析报告详解(一)
    查看>>
    oracle tirger_在Oracle中,临时表和全局临时表有什么区别?
    查看>>
    oracle where 条件的执行顺序分析1
    查看>>
    Oracle 中的 decode
    查看>>
    oracle 使用leading, use_nl, rownum调优
    查看>>
    oracle 修改字段类型方法
    查看>>
    oracle 内存参数示意图
    查看>>