- 给定两个整形变量的值,将两个值的内容进行交换。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 1, b = 2, c;
c = a;
a = b;
b = c;
printf("a的值是%d,b的值是%d\n", a, b);
system("pause");
return 0;
}
3.求10 个整数中最大值。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,a=0;
int arr[10];
printf("请输入10个数字:");
for (i = 0;i <= 9;i++)
{
scanf("%d", &arr[i]);
if (arr[i] >= a)
{
a = arr[i];
}
}
printf("十个数中最大数是%d\n", a);
system("pause");
return 0;
}
4.将三个数按从大到小输出。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main() {
//三个数由大到小输出
int a, b, c, t;
printf("请输入三个数:");
scanf("%d %d %d", &a, &b, &c);
if (a < b) {
t = b;
b = a;
a = t;
}
if (a < c) {
t = c;
c = a;
a = t;
}
if (b < c) {
t = c;
c = b;
b = t;
}
printf("%d %d %d\n", a, b, c);
system("pause");
return 0;
}
5.求两个数的最大公约数。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int m, n, temp, i;
printf("Input m & n:");
scanf("%d %d", &m, &n);
if (m < n) /*比较大小,使得m中存储大数,n中存储小数*/
{ /*交换m和n的值*/
temp = m;
m = n;
n = temp;
}
for (i = n; i > 0; i--)
if (m%i == 0 && n%i == 0)
{/*输出满足条件的自然数并结束循环*/
printf("The GCD of %d and %d is: %d\n", m, n, i);
break;
}
system("pause");
return 0;
}