関数とステートメント †関数 †
#include <stdio.h> void main(void) { printf("%d\n",Add(2,3)); }
#include<stdio.h> int Add (int x,int y); void main(void) { printf("%d\n",Add(2,3)); } int Add(int x,int y) { return x+y; } ステートメント †1. 入力された文字が2ならtwo 3ならthree それ以外ならother と表示するプログラムをifを使って書きなさい。 #include<stdio.h> void main(void) { char i; i = getchar(); if(i == '2') { printf("two"); }else if(i == '3'){ printf("three"); }else{ printf("other"); } } 2. 入力された文字が1ならone 2ならtwo 3ならthree それ以外ならother と表示するプログラムをswitchを使って書きなさい。 #include<stdio.h> void main(void) { char i; i = getchar(); switch(i) { case '1' : printf("one"); break; case '2' : printf("two"); break; case '3' : printf("three"); break; default : printf("other"); break; } } 3. 2の問題でbreak;を抜いたらどうなるか確かめなさい。 #include<stdio.h> void main(void) { char i; i = getchar(); switch(i) { case '1' : printf("one"); case '2' : printf("two"); case '3' : printf("three"); default : printf("other"); } } 4. 10回コンソールから一文字入力しそれを出力するプログラムをfor ,while ,do while を使ってそれぞれ書きなさい。 for文: #include<stdio.h> void main(void) { char i; i = getchar(); switch(i) { case '1' : printf("one"); case '2' : printf("two"); case '3' : printf("three"); default : printf("other"); } } while文: #include<stdio.h> void main(void) { int i = 0; char j; while(i < 10) { j = getchar(); putchar(j); i++; } } do while文: #include<stdio.h> void main(void) { int i = 0; char j; do { j = getchar(); putchar(j); i++; }while(i < 10); } |