有3个数a,b,c,由键盘输入,输出其中最大的数(多种方法合集)初学者必学

这篇文章展示了四种不同的C语言编程方法来找出三个整数中的最大值。方法包括交换变量值的比较,使用if-else语句,三元运算符以及cin/cout流进行输入输出。每种方法都通过比较关系确定最大值并将其打印出来。
摘要由CSDN通过智能技术生成

#include<stdio.h> //输出最大的数 方法1

int main()

{

int a, b, c, d;

printf("请输入三个数:");

scanf_s("%d,%d,%d", &a, &b, &c);

if (a < b)

{

d = a;

a = b;

b = d; //1.如果a小于b将a的值赋给b,b的值赋给a //如果a大于b则跳转到下一步

}

if (a < c) //2.如果a小于c将a的值赋给c,c的值赋给a //如果a大于b则跳转到下一步

{

d = a;

a = c;

c = d;

} //1.2.是确保a大于b、c,

printf("最大的数为:%d", a);

return 0;

#include<stdio.h> //输出最大数 方法2

int main()

{

int a, b, c;

printf("请输入三位数");

scanf_s("%d %d %d", &a, &b, &c);

if ((a > b && a > c))

printf("您输入的%d为最大值", a);

else if ((b > a && b > c))

printf("您输入的%d为最大值", b);

else

printf("您输入的%d为最大值", c);

return 0;

}

#include<stdio.h> //输出最大数 方法3 关系式1?关系式2:关系式3 普通写法

int main()

{

int a, b, c;

scanf_s("%d %d %d", &a, &b, &c);

(a > b && a > c) ? printf("最大数为:%d",a) : 0;

(b > a && b > c) ? printf("最大数为:%d",b) : 0;

(c > b && c > a) ? printf("最大数为:%d",c) : 0;

return 0;

}

#include<stdio.h> //输出最大数 方法4 关系式1?关系式2:关系式3 嵌套写法

int main()

{

int a, b, c;

scanf_s("%d %d %d", &a, &b, &c);

(a > b && a > c) ? printf("最大数为:%d", a) :(b > a && b > c) ? printf("最大数为:%d", b) : (c > b && c > a) ? printf("最大数为:%d", c) : 0;

return 0;

}

#include<iostream>

using namespace std;

//编写一个c程序,输入a,b,c三个值,输出其中最大者。 //输出最大数 方法5 和方法2异曲同工

int main()

{

int a, b, c, max;

cin >> a >> b >> c;

max = a;

if (b > max && b > c)

{

max = b;

cout << max << endl;

}

else if (c > max && c > b)

{

max = c;

cout << max << endl;

}

else

{

cout << max << endl;

}

return 0;

}

喜欢记得点个收藏关注

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值