输入一个正整数给变量n,求1到n的所有正整数之和。不正确的程序是()。
A: #includevoidmain(){inti,n,sum;scanf("%d",&n);for(i=1,sum=0;i<=n;i++)sum=sum+i;printf("%d",sum);}
B: #includevoidmain(){inti,n,sum=0;scanf("%d",&n);for(i=1;i<=n;i++)sum=sum+i;printf("%d",sum);}
C: #includevoidmain(){inti,n,sum;scanf("%d",&n);for(i=1;i<=n;i++)sum=sum+i;printf("%d",sum);}
D: #includevoidmain(){inti,n,sum;scanf("%d",&n);for(i=1,sum=0;i<=n;)sum=sum+i,i++;printf("%d",sum);}
A: #includevoidmain(){inti,n,sum;scanf("%d",&n);for(i=1,sum=0;i<=n;i++)sum=sum+i;printf("%d",sum);}
B: #includevoidmain(){inti,n,sum=0;scanf("%d",&n);for(i=1;i<=n;i++)sum=sum+i;printf("%d",sum);}
C: #includevoidmain(){inti,n,sum;scanf("%d",&n);for(i=1;i<=n;i++)sum=sum+i;printf("%d",sum);}
D: #includevoidmain(){inti,n,sum;scanf("%d",&n);for(i=1,sum=0;i<=n;)sum=sum+i,i++;printf("%d",sum);}
举一反三
- 下面程序可以正确求出1+2+3+。。。+n的和。() #include intmain() { inti=1,sum=0,n; scanf(“%d”,&n); while(i<=n) sum=sum+i; ++i; printf("sum=%d",sum); return0; }
- int i=1,sum=0,n; scanf ("%d",&n) ; while (i<=n) sum=sum+I;
- main(){inti=1,sum;while(i<10);{sum=sum+i;i++;}printf(i=%d,sum=%d,i,sum);}
- 以下程序的输出是() int main(){ int i,sum=0; for(i=0;i<=100;i++)sum=sum+i; printf("sum=%d\n",sum); return 0; }
- 下面程序可以正确求出1+2+3+。。。+n的和。() #include int main( ) { int i = 1,sum = 0,n; scanf(“%d”,&n); while (i <= n) sum = sum + i; ++i; printf("sum=%d