“*”菱形打印程序
闲来无事,还记得以前课本上的那个课后习题,那就编个小程序玩玩。
1 //date:2013/8/14 2 //designer :pengxiaoen 3 //function: just for fun 4 5 #include "stdio.h" 6 #include "math.h" 7 int main () 8 { 9 int i,j,k,r; 10 int t,temp1,temp2; 11 //------------------------------------------------------------- 12 printf ("please enter a odd number between 3 and 30 : "); 13 scanf("%d",&r); 14 while ((r<3) || (r>30) || ((r+1)%2) ) 15 { 16 printf("\n error \n\n"); 17 printf ("please enter a odd number between 3 and 30 : "); 18 scanf("%d",&r); 19 } 20 21 t = (r-1)/2; 22 temp1 = t+1; 23 //-------------------------------------------------------------- 24 for(i=0; i<r;i++) 25 { 26 temp1--; 27 for(j=0; j<=abs(temp1);j++) printf(" "); 28 29 if(i<=t) temp2 = 2*i+1; 30 else temp2 = 2*(r-i)-1; 31 for(k=0; k<temp2;k++) printf("*"); 32 33 printf("\n"); 34 35 } 36 system("pause"); 37 }
执行结果