小编Sar*_*_Xx的帖子

找到两个x1,x2之间的素数总数,找不到错误?

我需要找到两个数字之间的素数之和,比如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)

c++ loops function

3
推荐指数
1
解决办法
122
查看次数

为什么在计算某些东西时我们会在计数中加1?

我想知道,为什么我们使用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)

c++ loops if-statement boolean function

1
推荐指数
1
解决办法
458
查看次数

我的功能实现有什么问题吗?

我需要编写一个将接受用户输入并附加以下公式的函数,但是我一直得到错误的输出并且看起来我的代码中的逻辑错误是什么

这是我的代码:

#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)

在此输入图像描述

c++ arrays loops function

0
推荐指数
1
解决办法
159
查看次数

标签 统计

c++ ×3

function ×3

loops ×3

arrays ×1

boolean ×1

if-statement ×1