CMPT-101 Lecture Notes - Lecture 9: Negative Number, Prime Number

63 views3 pages
10 Feb 2016
Course
Professor
#What is a program that can print the numbers 1 through 5?
print(1)
print(2)
print(3)
print(4)
print(5)
#This is not a very practical way of implementing repetition.
#What if we want to change that to be numbers 4-9?
#What if we want to print all the numbers 1-100?
#Instead, we use something called a counter.
counter=1 #initialize counter
while counter <= 5:
print (counter)
counter=counter+1 #update counter
#This will print the counter on numbers 1-5.
#Since on the first line we know the counter starts at 1...
#The second line says that the following should repeat
#as long as the counter is equal to or less than 5.
#The next line prints the counter.
#The last line updates the counter by adding 1 to it.
#Then, since the "while" command causes it to repeat until 5, it prints again.
minNum=int(input('Enter minimum number '))
maxNum=int(input('Enter maximum number '))
counter=minNum #initialize counter
while counter<=maxNum: #stopping condition
if counter%2==0:
print(counter)
counter=counter+1
#This program does the same as the one above, except it takes max and min from user.
#It then only prints odd or even numbers, depending on the value of the fifth line.
#"if counter %2==0:" means even, and "if counter %2==1:" means odd.
minNum=int(input('Enter minimum number '))
maxNum=int(input('Enter maximum number '))
counter=minNum #initialize counter
while counter<=maxNum: #stopping condition
if counter %2==0:
print(counter)
counter=counter+1
#The following program enters a starting balance and calculates balance
#after a number of years of interest.
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Class Notes
Textbook Notes
30 Verified Answers
Study Guides
1 Booster Class