C语言基础练习6


1.输入两个整数,输出其中值较大者。要求用函数来找到大数

#include


int main()
{
	int max(int x, int y);
	int a, b, c;
	printf("please enter two integer numbers:");
	scanf_s("%d,%d", &a, &b);
	c = max(a, b);
	printf("max is %d\n", c);
    return 0;
}

int max(int x, int y)
{
	int z;
	z = x > y ? x : y;
	return (z);
}
运行结果:

C语言基础练习6_第1张图片

2.输入两个实数,用一个函数求出它们之和

#include


int main()
{
	float add(float x, float y);
	float a, b, c;
	printf("please enter a and b:");
	scanf_s("%f,%f", &a, &b);
	c = add(a, b);
	printf("sum is %f\n", c);
    return 0;
}

float add(float x, float y)
{
	float z;
	z = x + y;
	return(z);
}
运行结果:

C语言基础练习6_第2张图片

3.输入4个整数,找出其中最大的数。用函数的嵌套调用来处理

#include


int main()
{
	int max4(int a, int b, int c, int d);
	int a, b, c, d, max;
	printf("Please enter 4 integer numbers:");
	scanf_s("%d %d %d %d", &a, &b, &c, &d);
	max = max4(a, b, c, d);
	printf("max=%d\n", max);
    return 0;
}

int max4(int a, int b, int c, int d)
{
	int max2(int a, int b);
	return max2(max2(max2(a, b), c), d);
}

int max2(int a, int b)
{
	return(a >= b ? a : b);
}
运行结果:

C语言基础练习6_第3张图片

4.求第5个学生的年龄

#include


int main()
{
	int age(int n);
	printf("NO.5,age:%d\n", age(5));
    return 0;
}

int age(int n)
{
	int c;
	if (n == 1)
		c = 10;
	else
		c = age(n - 1) + 2;
	return(c);
}
运行结果:
C语言基础练习6_第4张图片



你可能感兴趣的:(c语言,c语言,练习)