C ++编程帮助 [英] C++ Programming help

查看:98
本文介绍了C ++编程帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您创建一个程序,显示用户输入的两个数字之间的偶数整数之和。

You create a program that displays the sum of even integers between and including two numbers entered by the user ..

ex)2和7 = 12 (2 + 4 + 6)

ex) 2 and 7 = the sum of 12 (2+4+6)

这是我到目前为止! butt if u can just me the right direction that is helpful helpful

this is what i have so far! butt if u can just put me in the right direction that would be helpful

//Advanced30.cpp - displays the sum of the even integers between and 
//including two numbers entered by the user
//Created/revised by <your name> on <current date>

#include <iostream>
using namespace std;

int main()
{
    // declare variables
    int num1 = 0;
    int num2 = 0;
    int sum= 0;

    cout << "Enter the First Number:" << endl;
    cin >> num1;
    cout << "Enter the Second Number:" << endl;
    cin >> num2;

    if ( num1 > num2)
    { 
    cout << "Invalid entry. Final number must be less than the first number. Please try again." << endl;
    }
    for ( int sum = (((num1 + 1)/2)*2); num1 <= (((num2 + 1)/2)*2) ; sum = 2 + (((num1 + 1)/2)*2) )





    return 0;
}   //end of main function


推荐答案

在你的for循环应该是这样的。

In your for loop it should be like this.

double sum = 0.0;
for(i = num1; i <= num2; i++){
if(i % 2 == 0){  // Is our number even
   sum += i;
  }
}

就是这样,打印出sum。

That's it and it print out sum.

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

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