/* t2_2.c ( PASCAL講座 sample program ) for 文、while 文、do - while 文 コンパイル命令は gcc t2_2.c -o t2_2 実行命令は t2_2 */ #include #include #include #define RANDOMIZE() srand(time(NULL)) main(){ int a,i,n,x; char c,s[64],t[64]; RANDOMIZE(); /* 乱数の初期化 */ x=0; for(i=1;i<=10;i++) x=x+i; printf("1 + 2 + ... + 10 = %1d\n",x); printf("\n"); strcpy(s,""); for(c='a';c<='z';c++) sprintf(s,"%s%c",s,c); printf("%s\n",s); printf("\n"); strcpy(s,""); for(c='z';c>='a';c--) sprintf(s,"%s%c",s,c); printf("%s\n",s); printf("\n"); n=rand()%1000+1; printf("n = %1d\n",n); strcpy(s,""); while(n>0){ a=n%2; strcpy(t,s); if(a==0) sprintf(s,"0%s",t); else sprintf(s,"1%s",t); printf("%s\n",s); n=n/2; } printf("binary representation = %s\n",s); printf("\n"); n=rand()%1000+1; printf("n = %1d\n",n); strcpy(s,""); do{ a=n%2; strcpy(t,s); if(a==0) sprintf(s,"0%s",t); else sprintf(s,"1%s",t); n=n/2; }while(n>0); printf("binary representation = %s\n",s); }