← Back to all posts

Hello Repel community,
I am stuck on a CodeHS question titled: Better Sum. Can you guys please help me with this, it means the world to me, thank you <3
Here is the problem :)

Write a program that asks the user for two numbers. Using a for loop, add all of the numbers from the first to the second.
For example if the first number is 6 and the second number is 8 the result is 21 (6 + 7 + 8).
Print out the results when you are finished.

Coder100 (11092)

Sure! What language?

# JS

``````let firstnum = 6; // this is the start number (6)
let lastnum = 8; // this is the last number (8)

let sum = 0;

for (let i = firstnum; i <= lastnum; i ++) {
sum += i;
}

console.log(sum);``````

# Python

``````firstnum = 6 # this is the start number (6)
lastnum = 8 # this is the last number (8)

sum = 0

for i in range(firstnum, lastnum + 1):
sum += i

print(sum)``````

# C++

``````#include <iostream>

int main() {
int firstnum = 6; // this is the start number (6)
int lastnum = 8; // this is the last number (8)

int sum = 0;
for (int i = firstnum; i <= lastnum; i ++) {
sum += i;
}

std::cout << sum;
}``````
SimplyMiah (1)

Oh nvm I get it thanks

SimplyMiah (1)

Hey! :) For me, when I check my code, it tells me sume the numbers 100 to 200 and sum the numbers 0 to 1000 as well. How would I add this to my code?

Bookie0 (4591)

@SimplyMiah well if you use the code I just shared with you below

``````a = input("") # 1st number
b = input("") # 2nd number

c = b - a # how much times loop goes
for i in range(c):
sum += a # makes the sum
a += 1 # for getting the numbers in between

print(sum + b)``````

it wouldnt need any change, it would work just fine ;)

Bookie0 (4591)

hi there!

what language is this?

so first of all, to ask the user for 2 numbers you'll need an input. Then for the for loop, in python its `for i in range(x)` and replace x with the number you want or the variable you want.
you say the for loop has to add all the numbers from the first to second, so can maybe store the 1st number in a variable, say `a` and then increment `a` by one. Next add those two numbers together. do this until the 2nd number. To determine how many times the loop runs, you have to find out how many numbers between the 1st and the 2nd, so this can be achieved with a substraction.

then to print the results, use print.

Im assuming its in python, here is the brief program (spoiler alert)

``````a = input("") # 1st number
b = input("") # 2nd number

c = b - a # how much times loop goes
for i in range(c):
sum += a # makes the sum
a += 1 # for getting the numbers in between

print(sum + b)``````

So yea, if you need more help or if i was unclear, please tell me and I will try to help you ;)