请分别用while,do...while和for三种循环语句编写程序求1+3+5+7+..+101

如题所述

第1个回答  2013-12-13
int i =1;
int sum=0;
while(i<=101)
{
sum+=i;
i+=2;
}
输出 sum的值就是结果
---------------------------
int i=1;
int sum =0;
do
{
sum+=i;
i+=2;
} while(i<=101)
------------------------
int sum=0;
for(int i=1;i<=101;i+=2)
{
sum+=i;
}

...while和for三种循环语句编写程序求1+3+5+7+..+101
while(i<=101){ sum+=i;i+=2;} 输出 sum的值就是结果 --- int i=1;int sum =0;do { sum+=i;i+=2;} while(i<=101)--- int sum=0;for(int i=1;i<=101;i+=2){ sum+=i;}

...语句输出以下表达式的计算过程和结果: 1+3+5+7+…+99
while(n<100){ int s=0;s=s+n;n++;} 2:int s=0,n=1;do { s=s+n;n++ }(while n<100)3:for(int i=0;i<100;i++){ int s=0;s=s+i;}

2、分别用三种循环语句(while语句、do-while语句、for语句),实现求1...
int i=0;while(i<100){ sum += (i+1);i++;} printf("%d" , sum);i=0;do { sum += (i+1);i++;}while(i <=100)printf("%d" , sum);for(i = 1 ; i <=100 ; i++){ sum += i;} printf("%d" , sum);

求1-100的累加和分别用while,do...while和for这两种循环语句实现
while实现:int i=1;int s=0;while(0<i<=100){ s=s+i;i++;} do while实现:int i=1;int s=0;do{ s=s+i;i++;}while(0<i<=100)for实现:int s=0;for(int i=0;i<=100;i++){ s=s+i;}

...do-while语句、for语句)各编写一个程序实现求1 ~
do { total = total * i; i++; } while(i<=20); printf("result=%d\\n", total);}\/\/for写法#include <stdio.h>main(){ long total=1, i; for(i=1; i<=20; i++) total = total * i; printf("result=%d\\n", total);} ...

分别用 while, do while 和for 循环计算1+2+……+100的和,C语言编程
{ while(i<=100){ sum+=i;i++;} printf("%d",sum);} do...while语句:include<stdio.h> main(){ int i=0,sum=0;do { sum+=i;i++;}while(i<=100);printf("%d",sum);} for循环:include<stdio.h> main(){ int i,sum=0;for(i=0;i<=100;i++){ sum+=i;} printf(...

C语言编写程序,求1-3+5-7+...-99+101的值
用while如下 include<stdio.h> void main(){ int i = 1, sum1 = 1, j = 3, sum2 = 3, sum;while (i <= 101){ i = i + 4;sum1 += i;} while (j < 101){ j = j + 4;sum2 += j;} sum2 *= -1;sum = sum1 + sum2;printf("%d", sum);} 用for如下 includ...

分别用while、do…while和for循环语句打印出1到200间的整数
i=0;while(i<=200){ cout<<i;i++;} i=1;do { cout<<i;i++;} while(i<=200);for(i=1;i<=200;i++){ cout<

...分别用 if switch while dowhile for 循环求出1到100的和
if switch 是条件判断语句,在这儿得用循环呀!用for:int sum=0;for(int i=1;i<=100;i++){ sum+=i;} System.out.println(sum);用while:int sum=0;int i=1;while(i<=100){ sum=sum+i;i++;} System.out.println(sum);用 do-while:int sum=0;int i=1;do { sum=sum+i;i...

1. 分别用while for do- while循环语句编程求从1加到1000的和。
int sum = 0;for(i=1;i<=1000;i++){ sum += i;} Console.WriteLine(sum);int sum = 0;int iNum = 1;do { sum += iNum;} while(iNum<1000)Console.WriteLine(sum);int sum = 0;int iNum = 1;while(iNum<=1000)do { sum += iNum;} Console.WriteLine(sum);

相似回答