今天我来给大家在介绍5道关于C语言的小程序
1.给定两个整形变量的值,将两个值的内容进行交换。
#include<stdio.h>
#include<stdlib.h>
int main() {
int a = 10;
int b = 20;
int c = 0;
c = b;
b = a;
a = c;
printf("a=%d b=%d", a, b);
return 0;
system("pause");
}
下面给上道题加一个条件看看要怎么解决。
2.不允许创建临时变量,交换两个数的内容。
#include<stdio.h>
#include<stdlib.h>
int main() {
int a = 10;
int b = 20;
printf("a=%d b=%d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("a=%d b=%d", a, b);
return 0;
system("pause");
}
这样就实现了不创建临时变量完成要求啦。
3.求10 个整数中最大值。
#include<stdio.h>
#include<stdlib.h>
int main() {
int arr[] = { 20,50,45,41,89,47,63,99,48,10 };
int a = 0;
int max = arr[0];
for (a = 0; a <= 9; a++) {
if (arr[a] > max) {
max = arr[a];
}
}
printf("最大数为:%d", max);
return 0;
system("pause");
}
4.将三个数按从大到小输出。
#include<stdio.h>
#include<stdlib.h>
int main() {
int a, b, c;
int temp;
printf("请输入三个整数:");
scanf_s("%d %d %d", &a, &b, &c);
if (a < b) {
temp = a;
a = b;
b = temp;
}
if (a < c) {
temp = c;
c = a;
a = temp;
}
if (b < c) {
temp = c;
c = b;
b = temp;
}
printf("%d %d %d\n", a, b, c);
return 0;
system("pause");
}
5.求两个数的最大公约数。
#include<stdio.h>
#include<stdlib.h>
int main() {
int a = 0;
int b = 0;
printf("请输入两个整数:");
scanf_s("%d %d", &a, &b);
int c = a % b;
while (c != 0){
a = b;
b = c;
c = a % b;
}
printf("c = %d\n", b);
return 0;
system("pause");
}
至此,这五道小程序就写完啦。