for (a = 1; a <= 25; a++) {
printf("%d\n", a);
}
value %= 2;
switch (value) {
case 0:
puts("Event integer");
break;
case !0:
puts("Odd integer");
break;
}
#include <stdio.h>
int main(void) {
double salary = 22000;
int year;
for (year = 1; year <= 10; ++year){
salary += salary * 0.05;
}
printf("%4u%21.2f\n", year - 1, salary);
}
#include <stdio.h>
int main(void) {
double y;
for (y = 7.11; y <= 7.20; y += 0.01){
printf("%7.2f\n", y);
}
}
#include <stdio.h>
int main(void) {
int x;
for (x = 1; x <= 100; ++x){
if (x % 3 == 0) {
printf("%d\n", x);
}
}
}
#include <stdio.h>
int main(void) {
int x = 1;
while (x <= 10) {
printf("%d\n", x);
++x;
}
}
#include <stdio.h>
int main(void) {
int x;
int sum;
sum = 0;
for (x = 1; x <= 50; ++x){
sum += x * x;
}
printf("%d\n", sum);
}
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
7
12
17
22
27
2
6
10
14
18
30
24
18
22
17
12
7
2
程式碼:
#include <stdio.h>
int main(void) {
int x;
for (x = 1; x <= 13; x += 2){
printf("%u\n", x);
}
}
執行結果:
1
3
5
7
9
11
13
程式碼:
#include <stdio.h>
int main(void) {
int x;
for (x =2; x <= 17; x += 3){
printf("%u\n", x);
}
}
執行結果:
2
5
8
11
14
17
程式碼:
#include <stdio.h>
int main(void) {
int x;
for (x = 30; x >= -30; x-= 10){
printf("%d\n", x);
}
}
執行結果:
30
20
10
0
-10
-20
-30
#include <stdio.h>
int main(void) {
int x;
for (x = 15; x <= 55; x += 8){
printf("%d\n", x);
}
}
執行結果
15
23
31
39
47
55
程式碼:
#include <stdio.h>
int main(void) {
unsigned int x;
unsigned int y;
unsigned int i;
unsigned int j;
printf("%s", "Enter two unsigned integers in the range 1-20: ");
scanf("%d%d", &x, &y);
for (i = 1; i <= y; ++i){
for (j = 1; j <= x; ++j) {
printf("%s\n", "@");
}
puts("");
}
}
執行結果:
Enter two unsigned integers in the range 1-20: 3 5
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
將輸入兩個數字,第一個數字控制@
的次數,第二個數字控制要重複顯示相同次數@
的次數。
請撰寫一個會將咦連串整數相加並計算其平均值數的程式,設定以scanf
所讀取的第一個數值。底下是輸入列的範例:
7 678 234 315 489 536 456 367
其中7表示後面共有7筆資料要輸入。
程式碼:
#include <stdio.h>
int main(void) {
int sum;
int count;
double average;
int value;
int i;
sum = 0;
printf("%s", "輸入要輸入幾次:");
scanf("%d", &count);
for (i = 1; i <= count; ++i){
printf("%s", "輸入數值:");
scanf("%d", &value);
sum += value;
}
average = (float) sum / count;
printf("%.6f\n", average);
}
執行結果:
輸入要輸入幾次:7
輸入數值:678
輸入數值:234
輸入數值:345
輸入數值:489
輸入數值:536
輸入數值:456
輸入數值:367
443.571442
程式碼:
// celsius_to_fahrenheit.c
// 攝氏轉華氏Celsius to Fahrenheit
#include <stdio.h>
int main(int argc, char const *argv[]) {
int celsius;
float fahrenheit;
printf("%8s%11s\n", "Celsius", "Fahrenheit");
for (celsius = 30; celsius <= 50; celsius++) {
fahrenheit = ((float) 9 / 5) * celsius + 32;
printf("%8u%11.2f\n", celsius, fahrenheit);
}
}
執行結果:
Celsius Fahrenheit
30 86.00
31 87.80
32 89.60
33 91.40
34 93.20
35 95.00
36 96.80
37 98.60
38 100.40
39 102.20
40 104.00
41 105.80
42 107.60
43 109.40
44 111.20
45 113.00
46 114.80
47 116.60
48 118.40
49 120.20
50 122.00
請撰寫一個程式,計算及印出1到100之間所有7的倍數之和。
程式碼:
#include <stdio.h>
int main(void) {
int count;
int value;
int i;
count = 100;
for (i = 1; i <= count; ++i){
value = i % 7;
if (value == 0) {
/* code */
printf("%u\n", i);
}
}
}
執行結果:
7
14
21
28
35
42
49
56
63
70
77
84
91
98
請撰寫一個程式,計算並印出從1到100之間所有質數。
程式碼:
#include <stdio.h>
int main(void) {
int count;
int value;
int i;
int j;
count = 100;
for (value = 1; value <= count; ++value){
j = 0;
for (i = 1; i <= value && j <= 2; i ++) {
if (value % i == 0) {
j++;
}
}
if (j == 2) {
printf("%u\n", value);
}
}
}
執行結果:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
編寫一個程式,印出從1到使用者輸入之任何數字之間的所有自然數的總和、平方和以及立方和。
程式碼:
#include <stdio.h>
#include <math.h>
int main(void) {
int n;
int sum;
int square;
int cube;
int count;
sum = 0;
square = 0;
cube = 0;
printf("%s", "輸入一個非負號的整數:");
scanf("%d", &n);
printf("%s\t%s\t%s\n", "總和", "平方和", "立方和");
for (count = 1; count <= n; count++) {
sum += count;
square += pow(count, 2);
cube += pow(count, 3);
}
printf("%d\t%d\t%d\n", sum, square, cube);
}
執行結果:
輸入一個非負號的整數:100
總和 平方和 立方和
5050 338350 25502500
輸入一個非負號的整數:10
總和 平方和 立方和
55 385 3025
輸入一個非負號的整數:120
總和 平方和 立方和
7260 583220 52707600