CSE 1310 - Assignments

Not Graded Practice for Assignment 2


Exercise 1.

product = 1
num = input('Enter first  number:')
while int(num)!= 0:
    num= input('enter new number')
    product = product * int(num)
print('product = ', product)

The above program is meant to multiply series of integers input by the user until a 0 is entered. For example if the user introduces 10, later 12, and finally 0, the program should output 10x12 = 120.

Find the error and rewrite the program so that it gets the desired output. Also, explain exactly (only a few words are needed) why the code, as given above, is incorrect.


Exercise 2.

The following code should print the integers from 0 to 9. Find the error and correct it:

num = 0
while n < 10:
    print n
    n = n + 1

Exercise 3.

Write a program that prompts the user for his/her age and depending on this input age it will print one of the following messages:

Between 0 and 20 years: "You are too young".
Between 21 and 40 years: "You are middle-aged".
41 years and above: "You are old".


Exercise 4.

Write a program that prompts to the user for a integer number, but if the input entered is not integer (e.g., the user inputs a string), keeps asking until the user introduces an integer.
Example:
Please enter an integer: 'Hello World'
Error, please try again.
Please enter an integer: 5.15
Error, please try again.
Please enter an integer: 5
Success! 5 is an integer number

Exercise 5.

Factors of an integer number X are those integer numbers that multiplied together give as a result X. Write a program that prompts the user for a number and prints all its positive factors.

Example:

Input: 24

Output: 1, 2, 3, 4, 6, 8, 12, 24

Note that the numbers above are factors of 24 because 1x24 = 24, 2x12 = 24, 3x8 = 24, 4x6 = 24.


Exercise 6.

Write a program that prompts the user for an number n and outputs the sum from 1 to n.

Input: 7.
Output: 28.

Exercise 7.

Write a program that prompts the user for integers until 0 is entered. The program should output the number of positive values entered, as well as the number of negatives.

Example

Input: 1, 2, -5, -7, -9, 4, -1, 0.
Output: 
	   Positive integers = 3. 
	   Negative integers = 4. 

Exercise 8.

Write a program containing a pair of nested loops that displays the integer values 1-25, 5 numbers per row, with the columns aligned as showed below:

 1  2  3  4  5  
 6  7  8  9 10
11 12 13 14 15 
16 17 18 19 20 
21 22 23 24 25