c++编程,求100之内的自然数中奇数之和
答案:5 悬赏:60 手机版
解决时间 2021-12-03 07:52
- 提问者网友:泪痣哥哥
- 2021-12-02 22:51
c++编程,求100之内的自然数中奇数之和
最佳答案
- 五星知识达人网友:底特律间谍
- 2021-12-03 00:14
//用do while语句
#include
int main()
{
int i=1;
int sum=0;
do{
sum+=i;
i+=2;
}while(i<=99);
printf("100以内奇数和为sum=%d\n",sum);
return 0;
}
//用for语句
#include
int main()
{
int i,n;
int sum=0;
for(i=1;i<=50;i++){
n=2*i-1;
sum=sum+n;
}
printf("100以内奇数和为sum=%d\n",sum);
return 0;
}
//用while语句
#include"stdio.h"
int main()
{
int i=1,n;
int sum=0;
while(i<=100)
{
if(i%2==0)//判断奇偶性
sum=sum+(i-1);
i++;
}
printf("100以内奇数和为sum=%d\n",sum);
return 0;
}
#include
int main()
{
int i=1;
int sum=0;
do{
sum+=i;
i+=2;
}while(i<=99);
printf("100以内奇数和为sum=%d\n",sum);
return 0;
}
//用for语句
#include
int main()
{
int i,n;
int sum=0;
for(i=1;i<=50;i++){
n=2*i-1;
sum=sum+n;
}
printf("100以内奇数和为sum=%d\n",sum);
return 0;
}
//用while语句
#include"stdio.h"
int main()
{
int i=1,n;
int sum=0;
while(i<=100)
{
if(i%2==0)//判断奇偶性
sum=sum+(i-1);
i++;
}
printf("100以内奇数和为sum=%d\n",sum);
return 0;
}
全部回答
- 1楼网友:轻熟杀无赦
- 2021-12-03 02:29
int i=1;
int sum=0;
do
{
sum+=i;
i+=2;
}while(i<=99)
int sum=0;
do
{
sum+=i;
i+=2;
}while(i<=99)
- 2楼网友:渊鱼
- 2021-12-03 02:19
....还用3个分开写
- 3楼网友:等灯
- 2021-12-03 01:16
//判断是否为奇数
int sum=0;
for(int i=1;i<100;i++)
{
if(i%2==0)
sum+=i;
}
return sum
int sum=0,i=0;
do
{
if(i%2==0)
sum=sum+i;
i++;
}while(i<=100)
return sum;
int sum=0;
for(int i=1;i<100;i++)
{
if(i%2==0)
sum+=i;
}
return sum
int sum=0,i=0;
do
{
if(i%2==0)
sum=sum+i;
i++;
}while(i<=100)
return sum;
- 4楼网友:轻熟杀无赦
- 2021-12-03 01:05
# include
main()
{int n=0,i;
for(i=1;i<=100;i++)
if(i%2!=0)
n=n+i;
printf("the resule is %d",n);
}
#include
main()
{int n=0,i=1;
do
{if(i%2!=0)
n=n+i;
i++;
}while(i<=100);
printf("the result is %d",n);
}
#include
main()
{int n=0,i=1;
while(i<=100)
{if(i%2!=0)
n=n+i;
i++;
}
printf("the result is %d",n);
}
main()
{int n=0,i;
for(i=1;i<=100;i++)
if(i%2!=0)
n=n+i;
printf("the resule is %d",n);
}
#include
main()
{int n=0,i=1;
do
{if(i%2!=0)
n=n+i;
i++;
}while(i<=100);
printf("the result is %d",n);
}
#include
main()
{int n=0,i=1;
while(i<=100)
{if(i%2!=0)
n=n+i;
i++;
}
printf("the result is %d",n);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |