C language implementation of three Numbers from small to large and output method example

  • 2020-06-15 10:03:28
  • OfStack

preface

This paper mainly introduces a function, arbitrary input 3 integers, programming to achieve the sort of these 3 integers from small to large. The following is not much, let's start with a detailed introduction

Implementation process:

(1) Define the data type. In this example, a, b, c and t are all basic integers.

(2) Use the input function to obtain any 3 values and assign them to a, b and c.

(3) if statement is used for conditional judgment. If a is greater than b, then the values of a and b are exchanged by means of the intermediate variable t, and then a and c, b and c are compared, and the final result is the ascending order of a, b and c.

(4) Use the output function to output the values of a, b and c in turn.

(5) The program code is as follows:


#include <stdio.h>
int main()
{
 int a,b,c,t; /* define 4 A basic integer variable a , b , c , t*/
 printf(" Please enter the  a,b,c:\n"); /* Ordinary characters in double quotes are printed as is and wrapped */
 scanf("%d,%d,%d",&a,&b,&c); /* Enter any 3 The number of */
 if(a>b) /* if a Is greater than b, With intermediate variables t implementation a with b The value of the swap */
 {
  t = a;
  a = b;
  b = t;
 }
 if(a>c) /* if a Is greater than c, Change the scene in the middle t implementation a with c The value of the swap */
 {
  t = a;
  a = c;
  c = t;
 }
 if(b>c) /* if b Is greater than c, With intermediate variables t implementation b with c The value of the swap */
 {
  t = b;
  b = c;
  c = t;
 }
 printf(" The order of the Numbers is: \n");
 printf("%d,%d,%d",a,b,c); /* Output function sequential output a , b , c The value of the */
 return 0;
}

Operation results:

[

linuxidc@linuxidc:~/linuxidc.com$ ./www.linuxidc.com

Please enter the a b, c:

177,999,678

The order of the Numbers is:

177,678,999

]

Note:

This example USES scanf("%d%d%d",&a,&b,&c); Get any number of 3 from the keyboard. When entering data, Enter key and Tab key can also be used, instead of using a comma as a delimiter between two data. If you enter a function in format scanf("%d,%d,%d",&a,&b,&c) Enter the data and use ", "as the space between the two data points.

conclusion


Related articles: