C语言程序练习题7
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
1.将输入的字符串反转并输出结果:
#include <stdio.h>
#include <string.h>
int main() {
char str[100], temp;
int i, j;
printf("Enter a string: ");
scanf("%s", str);
j = strlen(str) - 1;
for (i = 0; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
printf("Reverse of the string: %s\n", str);
return 0;
}
2.求解平方根:
#include <stdio.h>
#include <math.h>
int main() {
double num, root;
printf("Enter a number: ");
scanf("%lf", &num);
root = sqrt(num);
printf("Square root of %.2lf is %.2lf\n", num, root);
return 0;
}
3.使用递归函数计算斐波那契数列:
#include <stdio.h>
int fib(int n) {
if (n == 0 || n == 1) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
int main() {
int n, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("%d ", fib(i));
}
printf("\n");
return 0;
}
4.使用指针交换两个变量的值:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |