笔记12:if语句编程练习(打印输出三个数据中的最小值)

输入三个数,分别放入变量x,y,z中
打印输入数据中最小的那一个数

解决方案1

定义中间变量 t
1.比较x和y的大小关系,将较小的值赋值给t
2.比较t和z的大小关系,将较小的值赋值给t
3.t 中保存的就是3个数中的较小值

(直接t=z)

代码部分:

#include<stdio.h>
int main( )
{
	int x = 0;
	int y = 0;
	int z = 0;
	int t = 0;

	printf("Input 3 integer: ");
	scanf("%d%d%d", &x, &y, &z);
	getchar();
	
	if(x<y)
	{
		t=x;
	}
	else
	{
		t=y;
	}

	if(t>z)
	{
		t=z;
	}

	printf("The smallest is:%d\n",t);
	getchar();
	return 0;
}

运行结果:

解决方案2

代码:

#include<stdio.h>
int main( )
{
	int x = 0;
	int y = 0;
	int z = 0;
	
	printf("Input 3 integer: ");
	scanf("%d%d%d",&x,&y,&z);
	getchar();
	
	if(x < y)
	{
		if(x < z)
		{
		printf("The smallest is:%d\n",x);
		getchar();
		}
		else
		{
		printf("The smallest is:%d\n",z);
		getchar();
		}
	}
	else
	{
		if(y < z)
		{
		printf("The smallest is:%d\n",y);
		getchar();
		}
		else
		{
		printf("The smallest is:%d\n",z);
		getchar();
		}
	}
	
	getchar();
	return 0;
}

运行结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值