例題2 /* e2.c 2004/10/11 */ #include <stdio.h> int main(void) { int a,b; a = 1; printf("a = %d\n",a); b = a; printf("b = %d\n",b); a = 2; printf("a = %d\n",a); return 0; }
48 :
例題3 なるほど、変数は初期化しないと0じゃなくて「なにか」がでちゃうのね。 /* e3.c 2004/10/11 */ #include <stdio.h> int main(void) { int a; printf("a = %d\n",a); return 0; }
49 :
/* 3.c 2004/10/11 */ #include <stdio.h> int main(void) { int number; number = 2 + 1; printf("第%d号機\n",number); return 0; }
50 :
/* 練習4.c 2004/10/11 */ #include <stdio.h> int main(void) { int number; number = 3; number = number + 1; printf("練習 %d 題目\n",number); return 0; }
51 :
/* e5.c 2004/10/12 */ #include <stdio.h> int main(void) { int a,b,c; a = 2; b = 5; c = a * b; printf("Ans = %d\n",c); return 0; }
52 :
/* e6.c 2004/10/12 */ #include <stdio.h> int main(void) { float a,b,c; a = 0.2; b = 0.5; c = (a * b) / 2; printf("Ans = %f\n",c); return 0; }
53 :
/* p1.c 2004/10/12 */ #include <stdio.h> int main(void) { int a,b,c; a = 2; b = 5; printf("a = %d\n",a); printf("b = %d\n",b); printf("値の入れ替え\n"); c = a; a = b; b = c; printf("a = %d\n",a); printf("b = %d\n",b); return 0; }
54 :
/* p2.c 2004/10/12 */ #include <stdio.h> int main(void) { double r,pi,l,a; r = 10; pi = 3.141592654; l = 2 * pi * r; a = pi * r * r; printf("半径 = %f\n",r); printf("円周 = %f\n",l); printf("面積 = %f\n",a); return 0; }
55 :
/* p3.c 2004/10/12 */ #include <stdio.h> int main(void) { int a; a = 90; printf("%dmin = %dh%dm\n", a, (a/60), (a%60)); return 0; }
56 :
/* c4.c 2004/10/12 */ #include <stdio.h> int main(void) { int a; a = 1; printf("a = %d\n", a); if (a < 5){ printf("Statement 1\n"); } if (a > 5){ printf("Statement 2\n"); } else { printf("Statement 3\n"); } if (a > 5){ printf("Statement 4\n"); } else if (a < 0){ printf("Statement 5\n"); } else { printf("Statement 6\n"); } return 0; }
57 :
/* c5.c 2004/10/13 */ #include <stdio.h> int main(void) { int a, b; a = 8; b = 2; printf("a = %d\n",a); printf("b = %d\n",b); if (a > 0 && a < 10){ printf("a > 0 かつ a < 10\n"); } if (a > 5 && b == 2){ printf("a > 5 かつ b = 2\n"); } if (a != 0 || b == 5){ printf("a ≠ 0 または b = 5\n"); } return 0; }
58 :
/* e7.c 2004/10/14 */ #include <stdio.h> int main(void) { int a; a = -1; printf("a = %d\n", a); if (a >= 0){ printf("%d >= 0\n", a); }else{ printf("%d < 0\n", a); } return 0; }
59 :
/* e8.c 2004/10/14 */ #include <stdio.h> int main(void) { int a, b; a = 4; b = a % 2; printf("a = %d\n", a); if (b == 0){ printf("偶数\n"); }else{ printf("奇数\n"); } return 0; }
60 :
/* e81.c 2004/10/14 */ #include <stdio.h> int main(void) { int a; a = 4; printf("a = %d\n", a); if ((a%2) == 0){ printf("偶数\n"); }else{ printf("奇数\n"); }
return 0; }
61 :
/* c6.c 2004/10/14 */ #include <stdio.h> int main(void) { int a; a = 1; switch (a) { case 0: printf("a == 0\n"); break; case 1: printf("a == 1\n"); break; default: printf("a != 0 かつ a != 1\n"); } return 0; }
62 :
/* e9.c 2004/10/14 */ #include <stdio.h> int main(void) { int a; a = 1; switch (a) { case 0: printf("a == 0\n"); break; case 1: printf("a == 1\n"); default: printf("a != 0 かつ a != 1\n"); } return 0; }
63 :
/* e10.c 2004/10/14 */ #include <stdio.h> int main(void) { int a; a = 1; switch (a) { case 0: printf("×\n"); break; case 1: printf("○\n"); break; default: printf("?\n"); } }
/* e13.c 2004/10/15 */ #include <stdio.h> int main(void) { int a[100]; int i; for (i = 0; i<=99; i++) { a[i] = i + 1; printf("a[%d] = %d\n", i, a[i]); } return 0; }
75 :
/* c11-1.c 2004/10/15 */ #include <stdio.h> int main(void) { int i; int B[5]; B[0] = 50; B[1] = 51; B[2] = 52; B[3] = 53; B[4] = 54; for (i = 0; i<=4; i++) { printf("3年B組出席番号B[%d]番○○さんの体重は %d kgです。\n", i, B[i]); } return 0; }
/* e14-1.c 2004/10/16 */ #include <stdio.h> int main(void) { int i, j; int a[2][2]; a[0][0] = 0; a[0][1] = 1; a[1][0] = 2; a[1][1] = 3;
for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { printf("a[%d][%d] : %d\n", i, j, a[i][j]); } } return 0; }
79 :
/* p3.c 2004/10/17 */ #include <stdio.h> int main(void) { int a; a = 12; printf("a = %d\n", a); if (a >= 7 && a <= 12) { printf("elementary school\n"); }else if(a >= 13 && a <= 15) { printf("junior high school\n"); }else{ printf("unknown\n"); } return 0; }
80 :
/* p5.c 2004/10/17 */ #include <stdio.h> int main(void) { int i, j; for (i=1; i<=9; i++) { for (j=1; j<=9; j++){ printf("%d * %d = %d\n", i, j, (i*j)); } } return 0; }
81 :
/* p6.c 2004/10/18 */ #include <stdio.h> int main(void) { int a[10]; int i; for (i = 0; i<=5; i++) { a[i] = i * 20; printf("a[%d] = %d\n", i, a[i]); } return 0; }
関数の巻 /* c121.c 2004/11/24 */ #include <stdio.h> int main(void) { int a, b, c; a = 2; b = 8; c = a + b; printf("%d + %d = %d\n", a, b, c); a = 5; b = 5; c = a + b; printf("%d + %d = %d\n", a, b, c); return 0; }