对于循环奇数 [英] For Loop-Odd Numbers

查看:81
本文介绍了对于循环奇数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在尝试制作一个for循环来计算0到100范围内奇数的总和。

到目前为止我有

#include< iostream>

#include< iomanip>

使用命名空间std;

int main()


{

int odd = 1;

}

i需要一个FOR(东西,东西,某些东西)

和一个IF声明。

然后我不知道该怎么办其余部分... =(

i知道你是否添加就像从1到100的所有奇数一样,它将等于3000.

我怎么才能拿出奇数并用for循环将它们全部加在一起?


请回复如果您知道我的问题的答案

解决方案


嗯,我正在尝试制作一个for循环计算0到100范围内奇数的总和。

到目前为止我有

#include< iostream>

#include< iomanip>

使用命名空间std;

int main()


{

int odd = 1;

}

i需要一个FOR(东西,什么东西)

和一个IF声明。

然后我不知道该怎么做其余的部分... =(

i知道你是否添加了从1到1的所有奇数100它等于3000.

我怎么才能拿出奇数并用for循环将它们全部加在一起?


请回复如果你知道回答我的问题



算法很简单:

首先,你需要知道
之间有什么区别
- 偶数(2,4,6,8 ......等)

- 奇数(1,3,5,7 ......等)

{2,4,6,8 ...等}除以2.其余数字将为零

{1,3,5,7 ......等}除以2.剩下的号码是一个

你可以用来计算其余号码的算子除以模数(%)。

示例:


4%2 = 0:因为剩余数字为零(偶数)

2%3 = 2:因为2是我们之后的剩余号码2/3

3%2 = 1:奇数


- Go循环0到100(根据您的要求指示)

- 在循环的每个实例中,测试它是否为奇数/偶数(使用上面的模数)


希望这会有所帮助!!


/ looker



好​​吧,我''我试着制作一个for循环来计算0到100范围内奇数的总和。

到目前为止我有

#include< iostream> ;

#include< iomanip>

使用命名空间std;

int main()


{

int odd = 1;

}

i需要一个FOR(东西,什么东西)

和IF声明。

然后我不知道该怎么做剩下的部分... =(

i知道如果你添加像1到100的所有奇数一样它就等于3000.

我怎么才能拿到奇数并使用for循环将它们全部加在一起?


请回复如果您知道我的问题的答案



在你的for循环中第一个东西是一个整数(您想将其设置为= 0)。您的下一个是您希望for循环运行多长时间。所以你想要使用刚刚在第一个语句中创建的变量<或者>比某事(提示:x <= 100)。你的最后一个规定用于增加或减少变量。


你的for循环中你想使用模数除法(%运算符)来查看数字是否为奇数,如果是是奇数将它添加到一个变量(称之为总和),如果它甚至什么也不做。


你也可以让你的...循环开始于该范围内的第一个奇数,然后增加适当的数量,以便你永远不会得到偶数(提示:什么是3-1?5-3?7-5?两个连续奇数之间的差异总是......?)

Well, I''m tryin to make a for loop that computes the sum of the odd numbers in the range from 0 to 100.
So far i have
#include <iostream>
#include <iomanip>
using namespace std;
int main()

{
int odd = 1;
}
i need a FOR (something,something,something)
and an IF statement.
and then i dont know what to do for the rest of the part... =(
i know if you add like all the odd numbers from 1 to 100 it would equal 3000.
how would i just take the odd numbers and add them all together using a for loop?

Please Reply If you know the answer to my question

解决方案

Well, I''m tryin to make a for loop that computes the sum of the odd numbers in the range from 0 to 100.
So far i have
#include <iostream>
#include <iomanip>
using namespace std;
int main()

{
int odd = 1;
}
i need a FOR (something,something,something)
and an IF statement.
and then i dont know what to do for the rest of the part... =(
i know if you add like all the odd numbers from 1 to 100 it would equal 3000.
how would i just take the odd numbers and add them all together using a for loop?

Please Reply If you know the answer to my question

The algorithm is simple:
First of all, you need to know what is the difference between
- even number ( 2, 4, 6, 8...etc )
- odd number ( 1, 3, 5, 7....etc )
{2, 4, 6, 8...etc} divided by 2. The rest number will be Zero
{1, 3, 5, 7....etc} divided by 2. The rest number will be One
The operator you can use to calculate the the rest number from division is modulo ( % ).
Example:

4 % 2 = 0 : because the rest number is Zero ( even number )
2 % 3 = 2 : because 2 is the rest number after we 2/3
3 % 2 = 1 : odd number

- Go looping through 0 to 100 ( as what is instructed in your requirement )
- In each instance of your loop, test if it is odd/ even ( use modulo above )

Hope this will help!!

/looker


Well, I''m tryin to make a for loop that computes the sum of the odd numbers in the range from 0 to 100.
So far i have
#include <iostream>
#include <iomanip>
using namespace std;
int main()

{
int odd = 1;
}
i need a FOR (something,something,something)
and an IF statement.
and then i dont know what to do for the rest of the part... =(
i know if you add like all the odd numbers from 1 to 100 it would equal 3000.
how would i just take the odd numbers and add them all together using a for loop?

Please Reply If you know the answer to my question

in your for loop the first "something" is an integer (you want to set it to = 0). Your next is how long you want the for loop to run. So you want to use the variable you just created in the first statement to be < or > than something (hint: x <= 100). Your last stipulation is used to increment or decrement the variable.

Inside your for loop you want to use modulo division (% operator) to see if the number is odd or not, if it is odd add it to a variable (call it sum), if it is even do nothing.


You could also make your for...loop start at the first odd number in the range, and then increment by the proper amount so that you never get an even number (hint: What''s 3-1? 5-3? 7-5? The difference between two consecutive odd numbers is always...?)


这篇关于对于循环奇数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆