Sie sind auf Seite 1von 16

Algorithm Pseudocode Examples

Math 146 - Programming I

Spring 2017

Algorithm Pseudocode Examples


Programming Project Example

Write a program to find the sum of the first n integers, where the
value of n is provided by the user. That is, write a program to find
the following sum:

1 + 2 + ... + n

Algorithm Pseudocode Examples


Example 1

Algorithm Pseudocode:

def main():
n = eval(input("Enter a number: "))

mySum = 0

for i in range(n+1):
mySum += i

print("The sum is:", mySum)

main()

Algorithm Pseudocode Examples


Example 1

Algorithm Pseudocode:

def main():
n = eval(input("Enter a number: "))

mySum = 0

for i in range(n+1):
mySum += i

print("The sum is:", mySum)

main()

Score: 0/5

Algorithm Pseudocode Examples


Example 2

Algorithm Pseudocode:

Get a number from the user (call it n)


Add n to itself 10 times
Print out the result

Algorithm Pseudocode Examples


Example 2

Algorithm Pseudocode:

Get a number from the user (call it n)


Add n to itself 10 times
Print out the result

Score: 0/5

Algorithm Pseudocode Examples


Example 3

Algorithm Pseudocode:

Get a number from the user (call it n)


Add the numbers 1 + 2 + ... + n
Print the result

Algorithm Pseudocode Examples


Example 3

Algorithm Pseudocode:

Get a number from the user (call it n)


Add the numbers 1 + 2 + ... + n
Print the result

Score: 1/5

Algorithm Pseudocode Examples


Example 4

Algorithm Pseudocode:

Get a number from the user (call it n)


Loop over the numbers 1 through n
Add the current number to the sum
Print the result

Algorithm Pseudocode Examples


Example 4

Algorithm Pseudocode:

Get a number from the user (call it n)


Loop over the numbers 1 through n
Add the current number to the sum
Print the result

Score: 4/5

Algorithm Pseudocode Examples


Example 5

Algorithm Pseudocode:

Get a number from the user (call it n)


Set the starting sum to zero (call the variable mySum)
Use a while loop to loop over the numbers 1 through n
Add the current number to mySum
Print the result

Algorithm Pseudocode Examples


Example 5

Algorithm Pseudocode:

Get a number from the user (call it n)


Set the starting sum to zero (call the variable mySum)
Use a while loop to loop over the numbers 1 through n
Add the current number to mySum
Print the result

Score: 4/5

Algorithm Pseudocode Examples


Example 6

Algorithm Pseudocode:

Get a number from the user (call it n)


Set the starting sum to zero (call the variable mySum)
Use a for loop to loop over range(n)
Add the current number to mySum
Print the result

Algorithm Pseudocode Examples


Example 6

Algorithm Pseudocode:

Get a number from the user (call it n)


Set the starting sum to zero (call the variable mySum)
Use a for loop to loop over range(n)
Add the current number to mySum
Print the result

Score: 4.5/5

Algorithm Pseudocode Examples


Example 7

Algorithm Pseudocode:

Get a number from the user (call it n)


Set the starting sum to zero (call the variable mySum)
Use a for loop to loop over the numbers 1 through n
Add the current number to mySum
Print the result

Algorithm Pseudocode Examples


Example 7

Algorithm Pseudocode:

Get a number from the user (call it n)


Set the starting sum to zero (call the variable mySum)
Use a for loop to loop over the numbers 1 through n
Add the current number to mySum
Print the result

Score: 5/5

Algorithm Pseudocode Examples