Write, compile, and execute a C++ program to calculate the sum of the numbers from 1 to 100. 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. Your program should produce the display
The sum of the number is xxxx.
where the xxxx represents a field width of four digits and should be replaced by the sum computer by the program.

Recommended Answers

All 4 Replies

You need to read the rules of this forum, especially the rule that reads as follows:

Do provide evidence of having done some work yourself if posting questions from school or work assignments

Don't copy-paste your homework assignments (or other exercises) and expect people to simply give out a fully working answer for it, because that is against our rules and it is against your best interest.

This warning is good for all your other threads too (1 2 3). If you persist in spamming the forum with copy-pasted exercise questions, then we'd have to give you an infraction for it and delete the posts.

What have you tried to do towards solving this problem? Please show us what code you are working on for this and ask questions specific to what prevents you from solving it?

Just another person hoping that the internet is the answer to all of lives problems.

commented: Lol. I like this quote. Stealing it! +8

So, you have the math. Write the program!

  1    2    3    4 ....    n
  n  n-1  n-2  n-3 ....    1
============================
n+1  n+1  n+1  n+1 ...   n+1

So ... 2 * SUM = n*(n+1)
Thus       SUM = n*(n+1)/2

Can you write code to prompt and input an integer into a variable ...
(maybe label that int variable n) ?

Show what code you can do ...
then we can see where you are at
and be able to help you from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.