C语言-分支与循环语句练习

1,给与一组数据将数据对应的ASCLL值的字符输出来

复制代码
#include<stdio.h>

int main() {
	//给与一组数据将数据对应的ASCLL值的字符输出来
	int arr[] = { 73,32,99,110,32,100,111,32,105,116,33 };
	int i = 0;
	//sizeof(arr) 计算数组arr的大小,单位是字节
	//sizeof(arr[0]) 计算数组元素的大小
	int sz = sizeof(arr) / sizeof(arr[0]);

	while (i<sz) {
		printf("%c", arr[i]);
		i++;
	}
	return 0;
}

2,出生日期的输入输出

复制代码
#include<stdio.h>


出生日期的输入输出
int main() {
	//输入数据
	int year = 0;
	int month = 0;
	int date = 0;
	scanf("%4d%2d%2d", &year, &month, &date);
	//输出
	printf("year=%d\n", year);
	printf("month=%02d\n", month);//取两位,原本是空格,用0补齐
	printf("date=%02d\n", date);
	return 0;
}

3,一名学生成绩的输出

复制代码
#include<stdio.h>

//一名学生成绩的输出
int main() {
	int id = 0;
	float c = 0.0f;
	float math = 0.0f;
	float eng = 0.0f;
	//输入
	scanf("%d;%f,%f,%f", &id, &c, &math, &eng);
	//输出
	printf("The each subject score of NO.%d is %.2f , %.2f , %.2f.\n", id, c, math, eng);

	return 0;
}

4,printf函数的返回值

复制代码
#include<stdio.h>
//printf函数的返回值
int main() {
	int n = printf("Hello World!");
	printf("\n%d\n", n);
	return 0;
}

//int main() {
//	printf("printf(\"hello world!\\n\");\n");
//	printf("cout<<\"hello world!\"<<endl;\n");
//	return 0;
//}

5,四个数找最大值

复制代码
#include<stdio.h>
////四个数找最大值:法一
//int main() {
//	int arr[4] = { 0 };
//	int i = 0;
//	while (i < 4) {
//		scanf("%d", &arr[i]);
//		i++;
//	}
//	//找最大值
//	//假设第一个元素就是最大值
//	int max = arr[0];
//	i = 1;
//	while (i < 4) {
//		if (arr[i] > max) {
//			max = arr[i];
//		}
//		i++;
//	}
//	printf("%d\n", max);
//	return 0;
//}

//四个数找最大值:法二
int main() {
	int i = 1;
	int n = 0;
	int max = 0;
	scanf("%d", &max);
	//假设第一个元素就是最大值
	i = 1;
	while (i < 4) {
		scanf("%d", &n);
		if (n > max) {
			max = n;
		}
		i++;
	}
	printf("%d\n", max);
	return 0;
}

6,输出一个球体的体积 注意:精度问题用double和float的选择决定

复制代码
#include<stdio.h>
//输出一个球体的体积 注意:精度问题用double和float的选择决定
int main() {
	double r = 0.0;
	double v = 0.0;

	//float r = 0.0f;
	//float v = 0.0f;


	scanf("%lf", &r);
	v = 4 / 3.0 * 3.1415926 * r * r * r;
	printf("%.3lf\n", v);

	return 0;
}

7,计算体重指数

复制代码
#include<stdio.h>
//计算体重指数
int main() {

	int weight = 0;
	int high = 0;
	scanf("%d %d", &weight, &high);

	//计算

	float bmi = weight / (high / 100.0) / (high / 100.0);
	//输出
	printf("%.2f\n", bmi);
	return 0;
}

8,对于for循环,求循环次数

复制代码
#include<stdio.h>

//对于for循环,求这道题的循环次数
int main() {
	int i = 0;
	int k = 0;
	for (i = 0, k = 0; k = 0; i++, k++)//判断条件k=0是赋值,为假,根本都不会进入循环
		k++;
	return 0;
}
相关推荐
chao18984416 小时前
基于 SPEA2 的多目标优化算法 MATLAB 实现
开发语言·算法·matlab
赏金术士16 小时前
Kotlin 习题集 · 高级篇
android·开发语言·kotlin
楼兰公子17 小时前
buildroot 在编译rust时裁剪平台类型数量的方法
开发语言·后端·rust
知识领航员18 小时前
蘑兔AI音乐深度实测:功能拆解、实测表现与适用场景
java·c语言·c++·人工智能·python·算法·github
吴声子夜歌18 小时前
Go——并发编程
开发语言·后端·golang
ooseabiscuit18 小时前
Laravel4.x:现代PHP框架的奠基之作
java·开发语言·php
c1s2d3n4cs19 小时前
Qt模仿nlohmann::json进行序列化和反序列化
开发语言·qt·json
AiTop10019 小时前
Claude Code 推出 Agent View:命令行编程正式进入“多线程并发“时代
开发语言·人工智能·ai·aigc
jf加菲猫19 小时前
第21章 Qt WebEngine
开发语言·c++·qt·ui
码农-阿杰20 小时前
深入理解 synchronized 底层实现:从 HotSpot C++ 源码看对象锁与 Monitor 机制
开发语言·c++·