我需要找到两个数字之间的素数之和,比如x1和x2,但是我无法检测到什么是错误的?例如,如果我输入3和9我会得到15但我得到133!
#include <iostream>
using namespace std;
int prime(int n1, int n2)
{
int count =0;
bool prime = true;
for (n1; n1 < n2; n1++)
{
for (int i = 2; i < n1; i++)
{
if (n1 % i == 0) {
prime = false;
continue;
}
else
count++;
}
}
return count;
}
int main()
{
int n1, n2;
cout << " Enter values for n1 and n2 (n1 must be smaller than n2): ";
cin >> n1>>n2; …Run Code Online (Sandbox Code Playgroud) 我想知道,为什么我们使用count++而不是,例如count += 0,计算偶数的数量?
#include <iostream>
using namespace std;
int main()
{
int count = 1;
for (int i = 0; i <= 100; i++)
{
if (i % 2 == 0)
count += 0; // why it will give me only 1? as output
else
continue;
}
cout << "num of even: " << count << endl;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我需要编写一个将接受用户输入并附加以下公式的函数,但是我一直得到错误的输出并且看起来我的代码中的逻辑错误是什么
这是我的代码:
#include <iostream>
using namespace std;
int functX(int x) {
int fx = 1;
for (int i = 1; i <= 15; i++) {
fx *= (x + i);
}
return fx;
}
int main() {
int n;
cout << " Enter the value you want to sum" << endl;
cin >> n;
cout << functX(n) << endl;
system("Pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)