Question: Program 4: But I haven't taken Calculus yet! OK - we admit, this one might look nasty, but read on because it's not that bad.

Program 4: But I haven't taken Calculus yet! OK - we admit, this one might look nasty, but read on because it's not that bad. One of the things that computing folks do A LOT (especially CS students) is analyze how long a program takes to run. In the first step, something might take 1 unit of CPU time. The second step might take 2, the third 3 and so on. Mathematically, that would be expressed by the "summation" operator as this: This is may look complex, but all it's saying is that i goes from 1 to n and you sum all those numbers together. So, if (n = 10). i = 1 + 2 +3 +4 +5 +6 + 7 + 8 + 9 + 10 = 55 Fortunately, this reduces to a simple formula. When summing numbers from 1-n, the sum is: (n+1)*n/2 Why? It's all in how you "pair the numbers together. In the example above, we could pair (1+10)=11, (2+9)=11, (3+8)=11 and so on. That is, we have n/2 "pairs" of (n+1). Your task is, without using a loop (which we haven't covered yet), to write a program that asks the user for a number, then prints out the sum of 1 to that number. If you use a loop, you will receive no credit Sample Output: Enter a number: 3 The sum of the numbers between 1 and 3 is 6 Sample Output #2: Enter a number: 10 The sum of the numbers between 1 and 10 is 55 Sample Output #3: Enter a number: 15 The sum of the numbers between 1 and 15 is 120
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
