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

Intro to Algorithm Pseudocode

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)
386 views16 pages

Intro to Algorithm Pseudocode

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
  • Example 1: Basic Addition: Introduces a basic programming task to find the sum of the first n integers.
  • Example 1 Pseudocode: Provides pseudocode for calculating the sum of integers, illustrating basic loop structures.
  • Example 2: Repeated Addition: Describes an algorithm where a number is added to itself multiple times, explaining the repeated addition process.
  • Example 3: Sum Sequence: Explains how to compute the sum of a sequence from 1 to n using pseudocode.
  • Example 4: Loop Summation: Demonstrates a loop-based approach to summing numbers, emphasizing iteration over a numerical range.
  • Example 5: While Loop Sum: Covers using a while loop to sum integers, showcasing control structures in programming.
  • Example 6: For Loop Sum: Illustrates summation using a for loop to iterate over a defined range in pseudocode.
  • Example 7: Adaptive Summation: Involves summing numbers with a refined for loop approach, demonstrating adaptivity in code design.

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

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 us
Example 1
Algorithm Pseudocode:
def main():
n = eval(input("Enter a number: "))
mySum = 0
for i in range(n+1):
mySum += i
pri
Example 1
Algorithm Pseudocode:
def main():
n = eval(input("Enter a number: "))
mySum = 0
for i in range(n+1):
mySum += i
pri
Example 2
Algorithm Pseudocode:
Get a number from the user (call it n)
Add n to itself 10 times
Print out the result
Algorith
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
Example 3
Algorithm Pseudocode:
Get a number from the user (call it n)
Add the numbers 1 + 2 + ... + n
Print the result
Algor
Example 3
Algorithm Pseudocode:
Get a number from the user (call it n)
Add the numbers 1 + 2 + ... + n
Print the result
Score
Example 4
Algorithm Pseudocode:
Get a number from the user (call it n)
Loop over the numbers 1 through n
Add the current numb
Example 4
Algorithm Pseudocode:
Get a number from the user (call it n)
Loop over the numbers 1 through n
Add the current numb

You might also like