# Program for the sum of numbers

Page 1 of 1

## 4 Replies - 827 Views - Last Post: 26 June 2012 - 05:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=283631&amp;s=cffcb7b03cfd22dd0d467200aa3dee8c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 NicoleL

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 22-June 12

# Program for the sum of numbers

Posted 22 June 2012 - 08:45 PM

I am a student and this is my first time asking for help but I have reached a wall. The code below is what I have come up with. The question is: "If I am given a number, I need an application that will produce a sum of the numbers from 1 to the given number." My first error is that I am not sure if the loop is done the right way or if its placement is correct. I am struggling to understand pseudo code and because I am taking a course online, I do not have that physical guidance so I am looking for a more profound understanding from someone who explains it in more understandable terms.

```Main Module
// This program displays the sum of
Declare sum as float
Declare PositiveNumber as Integer
Declare  Number as Integer
Call input module
Call calculations module
Call output module
End Program
Input Module
Write " Enter number please:"
Input Number
Calculations Module
Loop while Number = 1
If Number > 1 Then
Set Sum = PositiveNumber + Number
Get Number
End Loop
Output Module
Write "The sum of all numbers is:" + Number

```

This post has been edited by GunnerInc: 22 June 2012 - 08:57 PM
Reason for edit:: fixed broken end code tag

Is This A Good Question/Topic? 0

## Replies To: Program for the sum of numbers

### #2 denting5

Reputation: 5
• Posts: 84
• Joined: 03-June 12

## Re: Program for the sum of numbers

Posted 25 June 2012 - 09:29 AM

Before I can help you, I need to know what language this is in. You did not specify, and this is the Other Languages forum you are posting in.

### #3 NicoleL

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 22-June 12

## Re: Program for the sum of numbers

Posted 25 June 2012 - 11:20 PM

I am in college and we are using a nondescript code from the textbook. It is supposed to be basic pseudocode writing and honestly I'm really not sure.

### #4 busyme12srv

• New D.I.C Head

Reputation: -5
• Posts: 44
• Joined: 18-June 12

## Re: Program for the sum of numbers

Posted 26 June 2012 - 12:40 AM

Your loop should be something like this :
Sum=0
Input Number

Loop while Number > 0

Set Sum = Sum + Number

Number = Number-1

End Loop

// If you enter 8 as number the the result will be 1+2+3+4+...+8

### #5 cfoley

• Cabbage

Reputation: 2199
• Posts: 4,622
• Joined: 11-December 07

## Re: Program for the sum of numbers

Posted 26 June 2012 - 05:19 PM

I've never understood why people teach pseudocode. It's useless to people and computers alike. You can't run it which means you can't test it which removes the feedback cycle which is an important part of learning to program. I've translated your program to python. I think it's every bit as intuitive to read as your original and you can actually run it and see what's happening:

```sum = 0
positiveNumber = 0
number = 0

def inputModule():
global number
inputString = input("Enter number please ")
number = int(inputString)

def calculationsModule():
global sum
while (number == 1):
sum = positiveNumber + number

def outputModule():
print("The sum of all numbers is:", number)

inputModule()
calculationsModule()
outputModule()
```

Running it gives:

```Enter number please 5
The sum of all numbers is: 0
```

So, what's wrong?

Here is an easy one to spot:
print("The sum of all numbers is:", number)
The answer should be contained in the variable sum so let's change it to
print("The sum of all numbers is:", sum)
and we get...

```Enter number please 5
The sum of all numbers is: 0
```

Which is no different. Trust me, we are getting closer. Let's add a couple print statement to the loop so we can see what is going on as the program runs:

```sum = 0
positiveNumber = 0
number = 0

def inputModule():
global number
inputString = input("Enter number please ")
number = int(inputString)

def calculationsModule():
global sum
while (number == 1):
sum = positiveNumber + number
print("sum is now", sum)

def outputModule():
print("The sum of all numbers is:", sum)

inputModule()
calculationsModule()
outputModule()
```

```Enter number please 5
The sum of all numbers is: 0
```

Oops what happened? We were expecting some extra output. In fact that's what would have happened if the loop was ever entered. The fact that there was no extra output means the loop was never entered. The condition while (number == 1) should be while (number >= 1) (In fact, that's what you had in your if statement I omitted). Let's make that change and see what we get:

```Enter number please 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5
sum is now 5

and so on.....
```

Something is happening but that something is an infinite loop. It's because the value of number never changes so it will always be >= 1. But we want to add up 5 + 4 + 3 + 2 + 1. You can see that each time round the loop we want to decrease the number. Let's do just that:

```sum = 0
positiveNumber = 0
number = 0

def inputModule():
inputString = input("Enter number please ")
global number
number = int(inputString)

def calculationsModule():
global number
global sum
print(number)
while (number >= 1):
sum = positiveNumber + number
print("sum is now", sum)
number = number - 1

def outputModule():
print("The sum of all numbers is:", sum)

inputModule()
calculationsModule()
outputModule()
```

```Enter number please 5
5
sum is now 5
sum is now 4
sum is now 3
sum is now 2
sum is now 1
The sum of all numbers is: 1
```

We are almost there but we can see that the sum isn't adding the numbers up. It's just storing the latest number. The problem is in this line:
sum = positiveNumber + number
The variable positiveNumber is never used apart from this so it always equals zero. That makes this line equivalent to:
sum = 0 + number
Now it's easy to see why the numbers don't get added. The correct line is:
sum = sum + number
This takes the current value of sum, adds the value of number and stores the result back in the sum variable. The number = number - 1 line I added earlier works in a similar way.

Now the output is this which you can see is correct:
```Enter number please 5
5
sum is now 5
sum is now 9
sum is now 12
sum is now 14
sum is now 15
The sum of all numbers is: 15
```

The final version of the code is below. I'll leave it to you to clean up by taking out the extra print statements.

```sum = 0
number = 0

def inputModule():
inputString = input("Enter number please ")
global number
number = int(inputString)

def calculationsModule():
global number
global sum
print(number)
while (number >= 1):
sum = sum + number
print("sum is now", sum)
number = number - 1

def outputModule():
print("The sum of all numbers is:", sum)

inputModule()
calculationsModule()
outputModule()
```

Anyway, I hope this helps. If you have to do your homework in pseudocode, I think it's worthwhile implementing it in a programming language too, just so you can test it.