Home>Article>Backend Development> How to output three numbers according to size in C language?

How to output three numbers according to size in C language?

烟雨青岚
烟雨青岚 Original
2020-06-10 15:18:32 8616browse

How to output three numbers according to size in C language?

Analysis and implementation of outputting three numbers by size in C language:

1. Analysis:The sorting of three numbers only requires three comparisons.

First, let these three numbers be a, b, and c respectively.

(1) Compare a with b, if a > b, exchange the values of a and b, compare a with c,

(2) If a > c, exchange a and c Compare the value b with c,

(3) If b > c, exchange the values of b and c

After three rounds of comparison, output a, b, c in order of size arranged.

2. Implementation:

#include  int main() { int a; int b; int c; int t; // 临时变量用于交换两变量的值 printf("请输入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("排序后a、b、c的值为%d、%d、%d", a, b, c); }

3. Running result:

请输入a、b、c的值,中间用空格隔开:4 1 7 排序后a、b、c的值为1、4、7

Recommended tutorial: "C Language Tutorial

The above is the detailed content of How to output three numbers according to size in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn