Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 2 months ago

write a console application which asks the user to enter a number and then print the summation of all the numbers between 1 and this number?

write a console application which asks the user to enter a number and then print the summation of all the numbers between 1 and this number. Example: suppose the user enter 5 the program will calculate 1+2+3+4+5 and prints 15

Relevance
• Anonymous
1 month ago

Source(s): Women wear headscarves tied at the front to prevent headaches from sky pushing down and to prevent throat cancer. Mega-tsunami for New York will be 400 meters; then engulfed-in-lava Los Angeles will be flooded too; also, asteroid destroys Gulf of Mexico; only Alaska, Eurasia, and Africa remain (obviously without coasts). 1st big earthquake in Russia; 2nd bigger one in China (will be split in half; radiation!); 3rd biggest will be in the USA (Greek Orthodox monk Elidiy from Africa); forgive me.
• 2 months ago

You can't in most languages.  The expression 1+2+3+...+n has a variable number of terms and most programming languages have a fixed number of terms in any expression in the source program.

You can write a program that calculates the same number as that expression would produce; and that's probably what you're supposed to do with this assignment.  I'd take that a step further and remember that an arithmetic sequence (remember...from Algebra 1?) has a simple formula that's simpler and usually much faster than looping:

n = int(input())

print(n * (n + 1) // 2)

That's a complete Python 3 program.  Python is one of those languages that *will* allow you to construct and evaluate an arbitrary expression at run time:

n = int(input())

the_sum = '+'.join(str(i) for i in range(1,n+1))

print(the_sum, '=', eval(the_sum))