DYING. I know this is super easy but can anyone help me out? Write a program, in any language, that prints the integers from 1 to 10, along with a cumulative sum of the integers printed so far. Your program's output should look like this: 1 1 2 3 3 6 4 10 5 15 6 21 7 28 8 36 9 45 10 55
First choose a language Second try to write the program, if you still get stuck put your code on dpaste.com and pass a link to us
I write in pseudo code so u can understand the general idea and then be able to use it in any language with proper conversions. sum=0 for i from 1 to 10 (starting with first value 1 and including 10) { print:i sum=sum +i print :sum endline i=i+1 }
I'll do a python solution, since Python looks like pseudocode :-D total = 0 # Total keeps track of the cumulative sum of numbers. for i in xrange(1,11): # Iterate through integers from 1 to 10. total += i # Increment the value of total by the value of i. print i, total # Outputs i and total in the specified format.
In bash: #!/bin/bash # print range 1 to 10, with running total total=0 for i in {1..10}; do total=$((total+i)) echo $i, $total done
Join our real-time social learning platform and learn together with your friends!