我是c ++的初学者。我需要编写一个代码,让我在输入的两个数字之间添加和相乘数字。示例:我输入2和4,加法应为9,乘法应为24.这就是我到目前为止所做的。
// sum and multi.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int num1;
cout << "Enter number: " << endl;
cin >> num1;
int sum=0;
int mul = 0;
for (int i = 0; i <= num1; i++)
{
sum += i;
while (i <= num1)
{
mul *= i;
}
}
cout << "sum is: " << sum << mul << endl;
return 0;
}
虽然它是加法部分,但它只从0到我输入的数字,它不做乘法部分。需要帮助!!!!
答案 0 :(得分:3)
您需要一系列数字,因此输入 两个 数字
int num1, num2;
cin >> num1 >> num2;
int sum=0;
int mul = 1; // Initialize mul to 1, since anything multiplied by 0, results in 0
for (int i = num1; i <= num2; i++) // Start from num1 and end to num2
{
sum += i;
//while (i <= num1) // Don't need another loop
//{
mul *= i;
//}
}