今天来学习for,while,do-while 的应用;
int main()
{
int iNumber=1;
int iSum=0;
for(;iNumber<=100;iNumber++)
{
iSum=iSum+iNumber;
}
printf("the result is %d\n");
iSum=0; iNumber=1; while(iNumber<=100) { iSum=iNumber+iSum; iNumber++; } printf("the result is %d\n"); iNumber=1; iSum=0; do { iSum=iSum+iNumber; iNumber++; } while(iNumber<=100; printf("the result is %d\n"); return 0;}for语句与while语句的应用;在这里应该注意while后应紧跟“;”
来用C语言做一个金字塔;
int main()
{
int i,j,k;
for(i=1;i<=5;i++) //在这里控制行数;
{
for(j=1;j<=5-i;j++)//这里打印空格;
printf(" ");//这里应注意空格也需输入;
for(k=1;k<=2*i-1;k++)//打印**;
printf("");
}
return 0;
}