Conditionals and Loops Ex.1 (Random month) ------------------- Write a program that randomly generates an integer between 1 and 12 and displays the English month name January, February, ..., December for the number 1, 2, ..., 12, accordingly. Hint: Use functions from Random library as described in the IOCheatSheet. Ex.2 (Interactive quitz) ------------------------ Write an interactive quiz. It should ask the user three multiple-choice or true/false questions about something. It must keep track of how many they get right, and print out a "score" at the end. Example: Are you ready for a quiz? Y Okay, here it comes! Q1) What is the capital of Albania? 1) Sofia 2) Tirana 3) Budapest 2 That's right! Q2) Can you store the value "dog" in a variable of type int? 1) yes 2) no 1 Sorry, "dog" is a string. ints can only store numbers. Q3) What is the result of 6+4/2? 1) 5 2) 8 3) 4 2 That's correct! Overall, you got 2 out of 3 correct. Thanks for playing! Ex.3 (Use the && , || and ^ operators) -------------------------------------- Write a program that prompts the user to enter an integer and determines whether it is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not both. Here is a sample run of this program: Enter an integer: 10 Is 10 divisible by 5 and 6? false Is 10 divisible by 5 or 6? true Is 10 divisible by 5 or 6, but not both? true Ex.4 (Game: pick a card) ------------------------ Write a program that simulates picking a (random) card from a deck of 52 cards. Your program should display the rank ( Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King ) and suit ( Clubs, Diamonds, Hearts, Spades) of the card. Here is a sample run of the program: The card you picked is Jack of Hearts Ex.5 (Business: check ISBN-10) ------------------------------- An ISBN-10 (International Standard Book Number) consists of 10 digits: d1 d2 d3 d4 d5 d6 d7 d8 d9 d10. The last digit, d10, is a checksum, which is calculated from the other nine digits using the following formula: (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11 If the checksum is 10, the last digit is denoted as X according to the ISBN-10 convention. Write a program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN (including leading zeros). Your program should read the input as an integer. Here are 2 sample runs: Enter the first 9 digits of an ISBN as integer: 013601267 The ISBN-10 number is 0136012671 Enter the first 9 digits of an ISBN as integer: 013031997 The ISBN-10 number is 013031997X Ex.6 (Find the smallest n such that n^2 > 12000) ------------------------------------------------ Use a while loop to find the smallest integer n such that n^2 (n square) is greater than 12000. Ex.7 Count positive and negative numbers and compute the average of numbers) ---------------------------------------------------------------------------- Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a real number. Here is a sample run: Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is 3 The number of negatives is 1 The total is 5.0 The average is 1.25 Ex. 8 (Binary representation) ------------------------------ Write a program that asks for a positive integer until the uses enters one, and calculates the binary representation of that integer. Hint: Use the modulo operator % Example: Enter a positive integer: -3 Enter a positive integer: -88 Enter a positive integer: 8 The binary representation of 8 is 1000 Ex.9 (Reverse a string) ----------------------- Write a program that prompts the user to enter a string and displays the string in reverse order. Sample run: Enter a string: ABCD The reversed string is DCBA Ex.10 (Find the factors of an integer) ------ -------------------------------- Write a program that reads an integer and displays all its smallest factors in increasing order. For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5. Ex.11 (Best student) ---------------------- Write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the name of the student with the highest score. Ex.12 (Display pyramid) ------------------------ Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run: Enter the number of lines: 5 1 2 1 2 3 2 1 2 3 4 3 4 1 2 3 4 5 4 3 2 1 2 3 4 5