C语言程序设计,比较数字大小的实例

来源:百度知道 编辑:UC知道 时间:2024/06/27 11:52:06

下面是一个比较三个数的例子:

  #include <stdio.h>
  int main()
  {
  int t,a,b,c;
  scanf("%d%d%d",&a,&b,&c);
  if(a<b)
  {
  t=a,a=b,b=t;
  }
  if(a<c)
  {
  t=a,a=c,c=t;
  }
  if(b<c)
  {
  t=b, b=c, c=t;
  }
  printf("%d %d %d\n",c,b,a);
  return 0;
  }
  原理就是运用冒泡算法,把最大的数浮在最上面,而小的数就下沉,最后就输出。

#include<iostream.h>
void main()
{
int x=0,y=0,t=0;
cout<<"请输入要比较的两个数!第一个数:"<<end;
cin>>x;
cout<<"第二个数:"<<end;
cin>>y;
if (x>y)
then t=x;
else t=y;
cout<<"您输入的数中,最大的是:"<<t<<end;
}

#include<stdio.h>
void main()
{
float a,b,t;
scanf(“%f%f”,&a,&b);
if(a>b)
{
t=a;