Take a number N as input and output the sum of all numbers from 1 to N (including N). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

Take a number N as input and output the sum of all numbers from 1 to N (including N).

Code of given problem using python

1st Sep 2022, 3:59 AM
Anuj Dubey
Anuj Dubey - avatar
2 Answers
+ 6
Anuj Dubey what have you done?? or should I say, "Where is your attempt ?" There are many ways to perform the task but inorder to help you it would be better to see what approach you are attempting so we can guide you in the right direction. Please tag relevant language names only: https://code.sololearn.com/W3uiji9X28C1/?ref=app
1st Sep 2022, 4:31 AM
BroFar
BroFar - avatar
+ 3
Anuj Dubey , here is a short description how this task could be done: > take an input from user, convert it to integer value and store it in a variable e.g. 'max_num' > declare a variable for holding the running sum e.g. 'total' > run a for loop with a range object, that uses 'max_num' as an argument (if max_num should be included, we need to add +1) the range object generates numbers from 0 upto max_num, one at a time. this number is stored in the loop variable. inside the loop we add the loop variable to the variable 'total' > if the loop is done, 'total' holds ths sum of all numbers from the range object > print 'total'
1st Sep 2022, 2:43 PM
Lothar
Lothar - avatar