Write a C++ program to calculate the sum of the numbers from 300 to 600. The formula for calculating this sum is sum = (n / 2) × (2 × a + (n - 1) × d), where n = number of terms to be added, a = the first number, and d = the difference between each number and the next number (d = 1)
1
Expert's answer
2021-10-22T07:39:44-0400
#include <iostream>
using namespace std;
int main() {
// sum(1..n) = n * (n + 1) / 2 = (a_1 + a_n) * n / 2
cout << (300 + 600) * 301 / 2 << endl;
return 0;
}
Comments
Leave a comment