1、随机产生10个数存入一维数组a中,求这10数中的最大数并输出。
#include
#include
main()
{int a[10],i,max;
for(i=0;i<10;i++)
{a[i]=rand()%50;
max=a[0];
if(max
max=a[i];}
printf("%d\n",max);
}
41
Press any key to continue
2,从键盘上输入a与n的值,计算sum=a+aa+aaa+aaaa+…(共n项)的和。例a=2,n=4,则sum=2+22+222+2222. #include
#include
#include
main()
{ int a,n,i,sum=0,sum1;
printf("input two number:");
scanf("%d",&a); scanf("%d",&n);
sum1=a;
for(i=2;i<=n;i++)
{ sum1=sum1*10+a; sum=sum+sum1; }
printf("the result is %d\n",sum);}
input two number:2 4
the result is 2466
Press any key to continue
3,编写程序,输出100~500之间的所有素数,每行输出5个。 #include
void main()
{ int i,j,k,min=100,max=500,temp=1;
for(i=min;i<=max;i++)
{ for(j=2;j<=i/2+1;j++)
{ if(i%j==0) break; }
if (j > i/2+1){ temp++;
printf("%5d",i); }
if(temp==6)
{ temp=1; printf("\n"); }
}
}
101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
179 181 191 193 197
199 211 223 227 229
233 239 241 251 257
263 269 271 277 281
283 293 307 311 313
317 331 337 347 349
353 359 367 373 379
383 389 397 401 409
419 421 431 433 439
443 449 457 461 463
467 479 487 491 499
Press any key to continue
4,有一头母牛,它每年年初生一头小母牛,每头小母牛从第四个年头起,每年年初也生一头小母牛,编写程序,求第20年时,共有多少头牛。#include
int main()
{
int n,s[100],i,sum;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
s[0]=s[1]=s[2]=0;
s[3]=1;
for(i=1;i
{
s[3]=s[3]+s[2];
s[2]=s[1];
s[1]=s[0];
s[0]=s[3];
}
sum=s[0]+s[1]+s[2]+s[3];
printf("%d\n",sum);
}
return 0;
}
20
1873
5,编写程序,输出下列三角形:
1 1 1 1 1 1 1
1 1 1 1 1
1 1 1
1
#include
main()
{ int i,j,k;
for(i=0;i<4;i++)
{ for(k=1;k<=i;k++)
printf(" ");
for(j=0;j<7-i*2;j++)
printf("1");
printf("\n");
}
}
1111111
11111
111
1
Press any key to continue