Steps: 1. Use scanf() to input 3 numbers, the syntax is "scanf("%d%d%d",&a,&b,&c)"; 2. Use if to compare the sizes of the three numbers And sorted from small to large, the syntax "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;}"; 3. Use printf() to output the three sorted numbers, the syntax is "printf("%d %d %d",a, b,c)”.
The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.
In C language, you can use scanf(), if statement and printf() function to input 3 numbers and output them from small to large.
Implementation steps:
Step 1: Use the scanf() function to input 3 numbers
scanf Yes The abbreviation of scan format means formatted scan, that is, obtaining user input from the keyboard
scanf("%d%d%d",&a,&b,&c);
Step 2: Use the if statement to compare the sizes of three numbers and follow the steps fromsmallto大Sort
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; }
Step 3: Use the printf() function to output the sort The last three numbers
printf() is a C language standard library function, used to output the formatted string to the standard output. Standard output, that is, the standard output file, corresponds to the terminal screen. printf() is declared in the header file stdio.h.
printf("%d %d %d",a,b,c);
Implementation code: input three numbers: 3 2 1, output 1 2 3
#includeint main(){ int a,b,c,t; printf("请输入三个整数:\n"); scanf("%d%d%d",&a,&b,&c); if(a>b){ // 3 2 1 t=a; // t=3 a=b; // a=2 b=t; // b=3 } if(a>c){ t=a; // t=2 a=c; // a=1 c=t; // c=2 } if(b>c){ t=b; // t=3 b=c; // b=2 c=t; // c=3 } printf("%d %d %d",a,b,c); return 0; }
Related recommendations: "C Video Tutorial"
The above is the detailed content of How to input 3 numbers in C language and output them from small to large. For more information, please follow other related articles on the PHP Chinese website!