0% found this document useful (0 votes)
243 views16 pages

Pseudocode Example Slides

This document provides examples of algorithm pseudocode for calculating the sum of integers from 1 to n, where n is provided by the user. Each example is given a score out of 5 based on how complete and clear the pseudocode is. The final and highest scored example (score of 5/5) uses a for loop to iterate from 1 to n, adding each number to a running sum variable mySum before printing the final result.

Uploaded by

hasib_07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
243 views16 pages

Pseudocode Example Slides

This document provides examples of algorithm pseudocode for calculating the sum of integers from 1 to n, where n is provided by the user. Each example is given a score out of 5 based on how complete and clear the pseudocode is. The final and highest scored example (score of 5/5) uses a for loop to iterate from 1 to n, adding each number to a running sum variable mySum before printing the final result.

Uploaded by

hasib_07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like