如何在 c++ 中循环获取奇数整数的总和
How to get the sum of odd integers in loop in c++
伙计们,我正在尝试输出a和b之间所有奇数的总和。尽管给定了条件,但输出的数字总会小于"a"。
#include <iostream>
#include <string>
int main()
{
int a =0;
int b =0;
int c=0;
std:: cout <<"Enter a number: n";
std::cin >> a;
std:: cout <<"Enter a number: n";
std::cin >> b;
int sum=0;
for(c; c%2!=0, c >= a, c <=b ; c+=2)
{
std::cout << "This is one of the odd numbers between " << a << " and " << b << " : "<< c << std::endl;
sum +=c;
}
std::cout << "The sum is : " <<sum;
return 3;
}
用试试这个
for(c=a; c%2!=0 && c>= a && c <=b ; c++){ std::cout <<"这是 " <<a <<" 和 " <<b <<之间的奇数之一 " : "<<c <<std::endl; 总和 +=c;}
相关文章:
- 如何获取矢量循环的整数位置
- 获取我的基类以递增派生类对象整数
- 从两个 4x64 位整数数组中获取取模
- 如何获取进程的整数PID?
- 从 stdio 获取一行空格分隔的整数,而不知道它们是多少 (C++)
- 给枚举一个整数,并在 C++ 中将其相关值作为字符串获取
- 如何在 c++ 中正确从正整数中获取偶数和奇数?
- 快速扫描标准以获取整数不起作用
- 获取由空格分隔的未知大小的单行输入,并将其存储到整数数组中
- 只要用户输入,如何从用户那里获取整数?
- 如何交替从文件中获取一行数据和一个整数?
- 获取两个无符号整数 C++ 乘积的高 32 位的有效方法
- 如何从另一个类动态获取整数?
- 如何使用字符串和整数获取汽车类的整数结果
- 控制台在获取整数后暂停
- C++一次从文本文件中获取每 2 个整数作为对
- 在某些范围更新后获取整数数组的最终状态的有效算法是什么?
- 如何从从 MPU 陀螺仪发送的字节数组中获取整数
- 获取所有方式以两个整数的产品表示数字
- 如何在C 中获取整数数组的元素