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;
}
相关推荐
RuoZoe8 天前
重塑WPF辉煌?基于DirectX 12的现代.NET UI框架Jalium
c语言
祈安_11 天前
C语言内存函数
c语言·后端
郑州光合科技余经理13 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo12313 天前
matlab画图工具
开发语言·matlab
dustcell.13 天前
haproxy七层代理
java·开发语言·前端
norlan_jame13 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone13 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ40220549613 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
czy878747513 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
遥遥江上月13 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js