Introduction Ⅱ
Objective
- Write programs using different loop structures and techniques.
Exercise 1*
Write a program to print the sum of all numbers from 1 to 100.
Exercise 2*
Write a program to swap two variables.
Exercise 3*
Write a program to compute the sum of the digits of an integer.
Input an integer: 25
The sum of the digits is: 7
Exercise 4
Write a program to print the integers between 1 to 100 which are divisible by 3, 5, and by both 3 and 5.
Divisible by 3:
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99
Divisible by 5:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100
Divisible by 3 and 5:
15, 30, 45, 60, 75, 90
Exercise 5
Write a program that accepts four integers from the user and prints equal if all four are equal, and not equal otherwise.
Input first number: 25
Input second number: 37
Input third number: 45
Input fourth number: 23
Numbers are not equal!
Exercise 6
Write a program that requests the user to type the time in seconds and displays it in the standard format of hours:minutes:seconds
.
Time in seconds: 3725
The time is 01:02:05
Exercise 7
Write a program that prompts for two numbers then prints the sum, difference, product, and quotient of those numbers as shown in the following sample output:
What is the first number? 10
What is the second number? 5
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2
The values received from the user will be strings. Ensure that you convert these values to numbers before doing the computation.
Exercise 8
Write a program to evenly divide pizzas. Prompt for the number of people, the number of pizzas, and the number of slices per pizza. Ensure that the number of pieces comes out even. Display the number of pieces of pizza each person should get. If there are leftovers, show the number of leftover slices.
How many people? 8
How many pizzas do you have? 2
How many slices per pizza? 8
8 people with 2 pizzas.
Each person gets 2 slices of pizza.
There are 0 leftover slices.
Exercise 9
Write a program that generates multiplication tables for the numbers 0 through 12, using a nested loop.
0 * 0 = 0
0 * 1 = 0
...
12 * 11 = 132
12 * 12 = 144
Exercise 10+
Write a program that takes two integer points from the user and then calculates the distance between them using the formula:
Enter 4 integer values for (x1, y1) and (x2, y2) respectively: 4 2 7 4
Distance between (4, 2) and (7, 4) is 3.61.
Exercise 11+
Write a program that keeps accepting numbers from the user until -1
is entered. The program then calculates the number of entered values, their sum, and their average. Format your output to show the average to 2 decimal places.
Enter a positive value or -1 to quit: 9
Enter a positive value or -1 to quit: 5
Enter a positive value or -1 to quit: 6
Enter a positive value or -1 to quit: 3
Enter a positive value or -1 to quit: 1
Enter a positive value or -1 to quit: -1
You entered 5 numbers. The sum is 24 and the average is 4.80.