Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active November 21, 2017 21:52
Show Gist options
  • Save rszewczyk/324890d420fbac628558b1864bab4761 to your computer and use it in GitHub Desktop.
Save rszewczyk/324890d420fbac628558b1864bab4761 to your computer and use it in GitHub Desktop.
CMSC 104 - Additional Practice Problems

Additional Practice Problems: Loops

Here are some optional practice problems to help you learn loops. You can implement all of them using either for or while loops - or try both!

Problem 1

Part A

Write a program that asks the user to enter a positive integer. It should continue to prompt the user until they enter a positive integer, at which point, it should print the number the entered.

Example Output:

Enter a positive integer: 0
Enter a positive integer: -3
Enter a positive integer: 5

You entered 5

Part B

Modify the program from Part A so that it informs the user of the problem if their number is invalid.

Example Output

Enter a positive integer: 0
0 is not a positive integer

Enter a positive integer: -3
-3 is not a positive integer

Enter a positive integer: 5

You entered 5

Part C

Modify the program from Part B so that user gets 5 attempts to print number. After the 5th attempt the program should inform the user they are out of tries and exit.

Example Output (success):

Enter a positive integer: 0
0 is not a positive integer

Enter a positive integer: -3
-3 is not a positive integer

Enter a positive integer: 5

You entered 5

Example Output (failure):

Enter a positive integer: 0
0 is not a positive integer

Enter a positive integer: -3
-3 is not a positive integer

Enter a positive integer: -1
-1 in not a positive integer

Enter a positive integer: -1
-1 in not a positive integer

Enter a positive integer: -1
-1 in not a positive integer

You ran out of tries

Part D

Modify the program from Part C so that each time it prompts the user it informs them how many tries they have remaining.

Example Output:

Enter a positive integer (5 attempts left): 0
0 is not a positive integer

Enter a positive integer (4 attempts left): -3
-3 is not a positive integer

Enter a positive integer (3 attempts left): 5

You entered 5

Part E

Modify the program from Part D so that the user must enter an even number. Make sure you modify the prompt so that it informs the user of this additional requirement. Also, make sure you inform the user if the number is not even.

Enter a positive, even integer (5 attempts left): -2
-2 is not a positive integer

Enter a positive, even integer (4 attempts left): 5
5 is not even

Enter a positive, even integer (3 attempts left): -3
-3 is not a positive integer
-3 is not even

Enter a positive, even integer (2 attempts left): 6

You entered 6

Problem 2

Part A

Write a program that prompts the user repeatedly for some number of integers. Then print the maximum value that the user entered. The program should prompt the user first for the number of integers they would like to enter (must be at least 2). As it prompts the user it should inform them of how many numbers they have left to enter.

Example output:

How many numbers  would you like enter? 0
You must enter at least 2 numbers.

How many numbers would you like enter? 4

Enter an integer (4 remaining): 6
Enter an integer (3 remaining): 9
Enter an integer (2 remaining): 1
Enter an integer (1 remaining): 5

The maximum value is 9

Part B

Modify your program from Part A so that it also prints the average of the values entered.

Example output:

How many numbers would you like enter? 0
You must enter at least 2 numbers.

How many numbers would you like enter? 4

Enter an integer (4 remaining): 6
Enter an integer (3 remaining): 9
Enter an integer (2 remaining): 1
Enter an integer (1 remaining): 5

The maximum value is 9
The average value is 5.25

Problem 3

Part A

Write a program that prompts the user to enter a positve integer. Then print a number of rows equal to that number. On the first row, print a single *. On each subsequent row, print two more *s than the previous row.

Example output:

Enter a positive integer: 0
0 is not a positive integer.

Enter a positive integer: 5
*
***
*****
*******
*********

Part B

Modify your program from Part A so that it centers each row on the row below it:

Example output:

Enter a positive integer: 0
0 is not a positive integer.

Enter a positive integer: 5
    *
   ***
  *****
 *******
*********

Part C

Modify your program from Part B so that it prompts the user for a number greater than 2. Then print a tree shape by printing the same triangle as before, but adding a trunk. The trunk should be centered and its width and height should be a constant proportion of the tree's width and height (the proportion is up to you).

Example output:

Enter an integer greater than 2: 1
1 is not greater than 2.

Enter an integer greater than 2: 5
    *
   ***
  *****
 *******
*********
   ***
   ***
Enter an integer greater than 2: 9
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************
      *****
      *****
      *****
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment