天天看點

用C語言求三個數的最大值與排序

用C語言求三個數的最大值與排序

1.用C語言求三個數的最大值

文法:if語句

代碼如下:

#include<stdio.h>
int main()
{
	int a, b, c;
	
	printf("請輸入三個整數(中間用空格間隔開來):");
	scanf("%d %d %d", &a, &b, &c);
	
	if (a > b )
		if(a > c)
			printf("%d", a);
		else
			printf("%d", c);	
	else
		if (b > c)
			printf("%d", b);
		else
			printf("%d", c);
	
	return 0;
 } 
           

2.用C語言求三個數的排序

文法:if語句

代碼如下:

#include<stdio.h>
int main()
{
	int a, b, c;
	int t;
	
	printf("請輸入三個整數(中間用空格間隔開來):");
	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("%d %d %d\n", a, b, c);
	
	return 0;
 } 
           

繼續閱讀