初学编程100个代码c
时间: 2023-09-21 07:03:29 浏览: 43
很高兴听到你想要学习编程!下面是一些适合初学者的简单代码示例:
1. 输出“Hello, world!”
```
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
```
2. 计算两个数之和
```
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("The sum is %d\n", sum);
return 0;
}
```
3. 判断一个数是否是偶数
```
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
```
4. 打印九九乘法表
```
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 9; i++) {
for (j = 1; j <= i; j++) {
printf("%d*%d=%d\t", j, i, i*j);
}
printf("\n");
}
return 0;
}
```
5. 求一个数的阶乘
```
#include <stdio.h>
int main() {
int num, i, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 1; i <= num; i++) {
fact *= i;
}
printf("%d! = %d\n", num, fact);
return 0;
}
```
6. 判断一个数是否是质数
```
#include <stdio.h>
int main() {
int num, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 2; i <= num/2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (num == 1) {
printf("1 is neither prime nor composite.\n");
} else {
if (flag == 0) {
printf("%d is prime.\n", num);
} else {
printf("%d is not prime.\n", num);
}
}
return 0;
}
```
7. 计算斐波那契数列
```
#include <stdio.h>
int main() {
int n, i, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
```
8. 排序一个整数数组
```
#include <stdio.h>
int main() {
int arr[100], n, i, j, temp;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
9. 计算圆的面积和周长
```
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area, perimeter;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
perimeter = 2 * PI * radius;
printf("Area = %f\nPerimeter = %f\n", area, perimeter);
return 0;
}
```
10. 将一个十进制数转换为二进制数
```
#include <stdio.h>
int main() {
int dec, rem, i = 0;
int bin[100];
printf("Enter a decimal number: ");
scanf("%d", &dec);
while (dec != 0) {
rem = dec % 2;
bin[i] = rem;
dec /= 2;
i++;
}
printf("Binary number: ");
for (i = i-1; i >= 0; i--) {
printf("%d", bin[i]);
}
printf("\n");
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)