c语言程序中如何比较四个数的大小?

c语言程序中如何比较四个数的大小?,第1张

按顺序先后比较四个数的大小然后找出最大值即可实现功能。

范例:

#include<stdio.h>

void main()

{

int a, b, c, d, tem

printf("input four numbers:\n")

scanf("%d,%d,%d,%d",&a,&b,&c,&d)

if(a>b)

{tem=aa=bb=tem}

if(a>c)

{tem=aa=cc=tem}

if(a>d)

{tem=aa=dd=tem}

if(b>c)

{tem=bb=cc=tem}

if(b>d)

{tem=bb=dd=tem}

if(c>d)

{tem=cc=dd=tem}

printf("%d,%d,%d,%d",a,b,c,d)

}

Ps:

常用排序方法如图:

上面的范例是以冒泡法编写的程序。

你if后面跟着的cout没有在

括号

里面,if为假只是不输出接着的cout,但{

}里面的会继续输出

#include<iostream>

using

namespace

std

int

main(void)

{

int

a,b,c,max,min

cout<<"请输入要比较的2个数:

"

cin>>a>>b

cout<<"1较大数

2较小数:

"

cin>>c

if

(c==1)

{

if

(a>b)

{

max=a

}

else

max=b

cout<<"所求的为较大数="<<max<<endl

}

if

(c==2)

{

if

(a<b)

{

min=a

}

else

min=b

cout<<"所求的为较小数="<<min<<endl

}

return

0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11033426.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存