Answer to Question #177267 in C++ for harichandra

Question #177267

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.


1
Expert's answer
2021-03-31T12:42:26-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Chala
02.02.23, 15:44

Its very nice

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS