Sum from 20 to 100. For loop (4860)

Hello :slight_smile:

307/5000

I am a beginner in programming and I have an exercise to do with the for loop.
I need to write the sum of the numbers 20-100 in a for loop like this.

20 -> 2

21 -> 3

22 -> 4

23 -> 5

24 -> 6

25 -> 7

26 -> 8

27 -> 9

28 -> 10

29 -> 11

30 -> 3

100 ->
I need instructions on how to do this.
Best regards

Take A Tour of Go. In particular, Flow control statements.

Thank you for your answer.

What function do I have to create to get the value on the right like these?
20 -> 2
21 -> 3
22 -> 4
23 -> 5

best regards

What function do you use on pen and paper for that relation? I can’t see any sense in that…

For me the sum of numbers from 20 to 100 is ((100 * (100 + 1))/2) - ((20 * (20 + 1))/2)

The exercise specified a solution using a for loop. You get an F on the test.

I provided you with a link to a Go for loop explanation with this example:

package main

import "fmt"

func main() {
	sum := 0
	for i := 0; i < 10; i++ {
		sum += i
	}
	fmt.Println(sum)
}

The example is close to what you want: it’s the sum of the numbers from 0 to 9. You want the sum of the numbers from 20 to 100. What changes to the example will give you the answer that you want?


I don’t understand what you are asking. It doesn’t seem relevant to your exercise.

Go does not have a Python-like range sequence type.

I can clearly solve that in a for loop as well. The problem is that I do not understand why 20 -> 2; 21 -> 3. Or how to use that when summing 20 + 21 + 22 + 23 + … + 100…

You have an off-by-one error. You are not including 20. The correct answer, given in the title of the question, is 4860:

package main

import (
	"fmt"
)

func sum(lo, hi int) int {
	if lo < 0 || lo > hi {
		return 0
	}
	return (hi*(hi+1) - lo*(lo-1)) / 2
}

func main() {
	fmt.Println(sum(0, 9)) // 4860
}

https://play.golang.org/p/KU5QHvJznE1

Thank you for help.

This is the exact wording of the task I received.

Printing the sum of all digits in the variable “a” in a for loop. So, assuming the initial value of the variable “a” equal to 20 and the upper bound of 100, I expect this output:

20 -> 2

21 -> 3

22 -> 4

23 -> 5

24 -> 6

25 -> 7

26 -> 8

27 -> 9

28 -> 10

29 -> 11

30 -> 3

31 -> 4

99 -> 18
100 -> 1

I did as in the answer, but I don’t know how to get these values, for example I marked “?” In the code below.

package main

import “fmt”

func main () {

sum: = 0
for a: = 20; a <101; a ++ {
sum + = a
fmt.Println (a, “->” “?”)

}

}
I have something like this:
20 ->
21 ->
22 ->
23 ->
24 ->
25 ->
26 ->
27 ->
28 ->
29 ->
30 ->
31 ->
32 ->
33 ->
34 ->
35 ->
36 ->
37 ->
38 ->
39 ->
40 ->
41 ->
42 ->
43 ->
44 ->
45 ->
46 ->
47 ->
48 ->
49 ->
50 ->
51 ->
52 ->
53 ->
54 ->
55 ->
56 ->
57 ->
58 ->
59 ->
60 ->
61 ->
62 ->
63 ->
64 ->
65 ->
66 ->
67 ->
68 ->
69 ->
70 ->
71 ->
72 ->
73 ->
74 ->
75 ->
76 ->
77 ->
78 ->
79 ->
80 ->
81 ->
82 ->
83 ->
84 ->
85 ->
86 ->
87 ->
88 ->
89 ->
90 ->
91 ->
92 ->
93 ->
94 ->
95 ->
96 ->
97 ->
98 ->
99 ->
100 ->

Sorry for my english, I’m learning.

[Prześlij opinię](javascript:void(0):wink:

[
Historia
](javascript:void(0):wink:

[
Zapisane
](javascript:void(0):wink:

[
Społeczność
](javascript:void(0):wink:

So you are searching for the sum of the digits for any number between a given upper and lower bound?

% and / with integer operands, you can use n % 10 to get ns last digit and n / 10 to drop it.

As this is obviously a homework, there will be no one presenting you a full solution (I hope).

I see two for loops:

package main

import "fmt"

func sumDigits(a int) int {
	if a < 0 {
		a = -a
	}
	sum := 0
	for a > 0 {
		sum += a % 10
		a = a / 10
	}
	return sum
}

func printSums(lo, hi int) {
	for a := lo; a <= hi; a++ {
		fmt.Println(a, "->", sumDigits(a))
	}
}

func main() {
	printSums(20, 100)
}

https://play.golang.org/p/cq2UPwuHS0T

20 -> 2
21 -> 3
22 -> 4
23 -> 5
24 -> 6
25 -> 7
26 -> 8
27 -> 9
28 -> 10
29 -> 11
30 -> 3
. . .
90 -> 9
91 -> 10
92 -> 11
93 -> 12
94 -> 13
95 -> 14
96 -> 15
97 -> 16
98 -> 17
99 -> 18
100 -> 1

Give a man a fish, and he won’t sleep hungry today, teach them to fish and they won’t suffer from hunger for a lifetime…

Anne Isabella Thackeray Ritchie (1837–1919) coined the adage in her novel Mrs. Dymond (1885).

“if you give a man a fish he is hungry again in an hour. If you teach him to catch a fish you do him a good turn.”

A novel is a work of fiction.

For non-fiction on fishing: The Compleat Angler by Izaak Walton.