Write a C++ program to calculate the sum of the numbers from 1 to 100. The formula for calculating this sum is sum = (n / 2) x (2 x a + (n - 1) x d), where n = number of terms to be added, a = the first number, and d = the difference between each number and the next number.
#include <iostream>
using namespace std;
int main()
{
int fir, las;
cout << "Enter first number: ";
cin >> fir;
cout << "Enter last number: ";
cin >> las;
cout << "The sum of the numbers from " << fir << " to " << las << " is " << (las + fir) * (las - fir + 1) / 2;
return 0;
}
Comments
Its very nice
Leave a comment