Latest web development tutorials

C Exercise Example 5

100 cases of classic C language 100 cases of classic C language

Title: Enter three integers x, y, z, please put three numbers from small to big output.

Program analysis: We find a way to put the smallest number x, the first x and y are compared, if x> y then the x and y values are exchanged, and then use the x and z are compared, if x> z x and z values ​​will be exchanged, so make x minimum.

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

#include <stdio.h>

int main()
{
    int x,y,z,t;
    printf("\n请输入三个数字:\n");
    scanf("%d%d%d",&x,&y,&z);
    if (x>y) { /*交换x,y的值*/
        t=x;x=y;y=t;
    }
    if(x>z) { /*交换x,z的值*/
        t=z;z=x;x=t;
    }
    if(y>z) { /*交换z,y的值*/
        t=y;y=z;z=t;
    }
    printf("从小到大排序: %d %d %d\n",x,y,z);
}

The above example output is:

请输入三个数字:
1
3
2
从小到大排序: 1 2 3

100 cases of classic C language 100 cases of classic C language