用C语言编写其中的题目
时间: 2024-10-23 21:08:59 浏览: 19
使用C语言 编写的 九九乘法表
好的,以下是每个实验题目的C语言实现:
### 题目1:生成并显示一个大写字母,并用其显示特定图案
**项目名**: ex55
**源文件名**: exc55.c
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char ch;
srand(time(NULL));
ch = 'A' + rand() % 26;
printf("Generated Character: %c\n", ch);
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ", ch);
}
printf("\n");
}
return 0;
}
```
### 题目2:找出并显示1000之内的所有完数
**项目名**: ex56
**源文件名**: exc56.c
```c
#include <stdio.h>
int isPerfectNumber(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum == num;
}
int main() {
printf("Perfect numbers within 1000:\n");
for (int i = 1; i <= 1000; i++) {
if (isPerfectNumber(i)) {
printf("%d\n", i);
}
}
return 0;
}
```
### 题目3:计算分数序列的前20项之和
**项目名**: ex57
**源文件名**: exc57.c
```c
#include <stdio.h>
double fractionSum(int n) {
double sum = 0.0;
int numerator = 1, denominator = 2;
for (int i = 0; i < n; i++) {
sum += (double)numerator / denominator;
numerator += 1;
denominator += 2;
}
return sum;
}
int main() {
double sum = fractionSum(20);
printf("The sum of the first 20 terms of the sequence is: %.10f\n", sum);
return 0;
}
```
### 题目4:计算Sn = a + aa + aaa + ... + aa...a
**项目名**: ex58
**源文件名**: exc58.c
```c
#include <stdio.h>
long long computeSn(int a, int n) {
long long sum = 0, term = 0;
for (int i = 0; i < n; i++) {
term = term * 10 + a;
sum += term;
}
return sum;
}
int main() {
int a, n;
printf("Enter the values of a and n: ");
scanf("%d %d", &a, &n);
long long result = computeSn(a, n);
printf("The value of S%d is: %lld\n", n, result);
return 0;
}
```
### 题目5:求最大公约数和最小公倍数(选做)
**项目名**: ex59
**源文件名**: exc59.c
```c
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
int main() {
int m, n;
printf("Enter two positive integers: ");
scanf("%d %d", &m, &n);
int g = gcd(m, n);
int l = lcm(m, n);
printf("GCD of %d and %d is: %d\n", m, n, g);
printf("LCM of %d and %d is: %d\n", m, n, l);
return 0;
}
```
### 题目6:求满足abcd + cadb = 7856的所有四位数对(选做)
**项目名**: ex510
**源文件名**: exc510.c
```c
#include <stdio.h>
void findPairs() {
for (int abcd = 1000; abcd <= 9999; abcd++) {
int a = abcd / 1000;
int b = (abcd / 100) % 10;
int c = (abcd / 10) % 10;
int d = abcd % 10;
int cadb = c * 1000 + a * 100 + d * 10 + b;
if (abcd + cadb == 7856) {
printf("Pair found: %d and %d\n", abcd, cadb);
}
}
}
int main() {
printf("Finding pairs of four-digit numbers such that abcd + cadb = 7856:\n");
findPairs();
return 0;
}
```
希望这些代码对你有帮助!如果有任何问题或需要进一步解释,请告诉我。
阅读全文