Home > Common Problem > body text

How to input 3 numbers in C language and output them from small to large

青灯夜游
Release: 2022-09-19 16:11:00
Original
54650 people have browsed it

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)”.

How to input 3 numbers in C language and output them from small to large

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);
Copy after login

Step 2: Use the if statement to compare the sizes of three numbers and follow the steps fromsmalltoSort

	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;	
	}
Copy after login

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);
Copy after login

Implementation code: input three numbers: 3 2 1, output 1 2 3

#include 
int 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;
}
Copy after login

How to input 3 numbers in C language and output them from small to large

How to input 3 numbers in C language and output them from small to large

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!