Skip to content

ProgrammingAssignment/Python-Programming-Assignment-Sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Problem 3: Triangular numbers

Use a for loop to compute the 10th triangular number. The nth triangular number is defined as 1+2+3+...+n. (You can also compute the nth triangular number as n*(n+1)/2. Use this formula to double-check that your loop is correct.)

Hint: This outline is an almost-complete solution. You only have to replace each ellipsis by an expression.

n = 10
triangular = 0
for i in ...:
    triangular = ...
print "Triangular number", n, "via loop:", triangular
print "Triangular number", n, "via formula:", n*(n+1)/2

Your code should correctly calculate the 11th, 12th, or any other triangular number just by changing the first line to set n to 11, 12, or any other number. Using the range function should help you accomplish this.

About

Assignments in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages