LOOPS - EXAMPLES (C, C++, JAVA)
Introduction to Loops in Programming
Loops in the C/C++ programming language
Loops in JAVA
When to use different types of loops
- For loop: Best used when you know in advance how many times you want to repeat a block of code. For example, to iterate through a known number of elements in an array or list.
- While loop: Used when the number of iterations is not known, but depends on a condition that can be changed inside the loop. It is ideal for situations when you work with uncertain inputs or events.
Edge cases: Specific conditions and exceptional situations
For example:
// Infinite loop
}
Empty loop: A loop that does not perform any action:
// Empty loop
}
1. The club you support
2. Printing a sequence of numbers
3. Writing numbers by rows and columns
Set the initial value of the control variable to 1, as the first odd number of the required number interval, and to change the control variable, set it to change by 2. That is. the third expression in the for loop should be i=i+2
After printing the number, using the if statement, put a new line, if the current number is divisible by 10
{
if(ciklus % 10 == 0)
printf("\n");
}
4. Celsius to Fahrenheit
Example 1
Input
10
15
Output
10[C] 50[F]
11[C] 51.8[F]
12[C] 53.6[F]
13[C] 55.4[F]
14[C] 57.2[F]
15[C] 59[F]
First enter the values of temperatures c1 and c2 using the cin command (C++) or scanf, and then create a for loop.
Put the control variable c, changes from c1 to c2 and it actually represents the temperature in Celsius.
In the body of the for loop, first calculate the current value in Fahrenheit using the formula:
T[oF]=T[oc]*9/5+32
Second, print these two values on the same line
using namespace std;
int main()
{
cin << c1 << c2;
/*Control variable c(temperature in Celsius) c changes from start c1 to end c2 with step 1
*/
for(double c=c1; c<=c2; c=c+1)
{
f=c*9.0/5.0+32;
cout << c << "[C] " << f << "[F]"<<endl;
5. Add even numbers
6. Average rating
7. Removing zeros from the right
8. Dividing a number with the same divisor
Explanation: 1st cycle: 36/3=12, 2nd cycle: 12/3=4, the remainder is 4 and the number of cycles is 2
9. Training
10. Cost sheet
11. Free fall simulation
12. Interest
Input
The initial amount of money, the amount of annual interest in percentages, and the number of years for which the amount of money with interest should be written.
Output
In the ith line, write the amount of money after i years.
13. The meeting of two cars
Simulate this movement for the entered car speeds, so that every 0.05s of movement, the current time, the traveled paths of both cars, and the current distance between them are printed. The distance between places A and B is entered by the user.
Set a meeting time.
14. Ski jumps - average
15. Stock market
Input
From the standard input, 5 integers from the interval [−10000,10000][−10000,10000] (amounts that the trader made on Monday, Tuesday, Wednesday, Thursday and Friday) are entered, each in a separate row.
Output
If the trader made a profit on a certain day (the amount is strictly greater than zero), on the standard output, in the first line, write the smallest amount of profit that was made during the week and in the second line, the designation of the day (MON, TUES, WED, CET or FRI) in which the smallest profit was achieved. If no earnings were made on any day, print a line containing only the character -.
Example 1
Input
3200
-420
-10
1350
5670
Output
1350
CET
Example 2
Input
-4700
-360
-1000
-1550
-3245
Output-
16. Countdown
Write a program that counts down from a given number to zero.
17. Show announcements
The beginning and end of a movie is known (time in hours and minutes). At regular time intervals, first at the beginning of the movie and then after every mm minutes, an announcement of the next show is shown in the corner of the screen. Write a program that prints the times at which this announcement is shown.
Input
The standard input contains the start time of the movie given in the form of two integers, each in a separate line representing the number of hours and the number of minutes, then, in the next line, the end time of the movie given in the same form and finally one integer representing the interval in minutes in which the announcement is displayed.
Output
On the standard output, display the broadcast times of the announcement in the form of h:m, each in a separate line.
Example
Input
12
0
13
53
15
Output
12:0
12:15
12:30 p.m
12:45
13:0
13:15
13:30
3:45
18. Read to the end of the entry
Whole numbers are entered from the input until the end of the input is reached. Write a program that displays how many numbers have been entered.
Input
Each line of standard input contains one integer. NOTE: when testing the program interactively, the end of the standard input is marked with the ctrl + z key combination if the Windows operating system is used, i.e. ctrl + d if using Linux operating system.
Output
In the first line of standard output, show how many numbers have been entered.
Example
Input
20
145
-23
12
456
13
Output
6
19. Reading the grades to the end of the entry and calculating the average
Input
Each line of standard input contains an integer representing a rate.
Output
Display the average grade in the first line of standard output.
Example
Input
5
2
4
4
2
6
Output
3.83
20. Juice warehouse
Input
In the first line of the standard input there is a natural number n (1≤n≤100) which represents the number of shelves in the warehouse. In the second, there is a natural number k (5≤k≤30) which represents the capacity of the crate. In each of the following nn lines there is a natural number, from the interval [5,500], which represents the number of bottles on each shelf.
Output
The smallest number of used crates is printed in one line of the standard output.
Example
Input
3
8
31
25
16
Output
10
21. The cheapest for a dinar
Input
From the standard input, three integers from the interval from 50 to 5000 are entered, which represent the prices in dinars for the three purchased products.
Output
Print an integer representing the total discounted price of those three products to standard output.
Example
Input
2499
3599
899
Output
6099
22. Broadcast announcements at regular time intervals
Input
The standard input contains the start time of the movie given in the form of two integers, each in a separate line representing the number of hours and the number of minutes, then, in the next line, the end time of the movie given in the same form and finally one integer representing the interval in minutes in which the announcement is displayed.
Output
On the standard output, display the broadcast times of the announcement in the form of h:m, each in a separate line.
Example
Input
12
0
13
53
15
Output
12:0
12:15
12:30 p.m
12:45
13:0
13:15
13:30
13:45
23. Reading to minus 1 or to the n-th
During the lesson, Pera played and cut paper squares. The teacher noticed this and gave him the task of calculating the total area of all the squares. Write a program that helps Perry do this. Pera, at the very beginning, enters the number "n" which he is sure is greater than or equal to the number of squares. After that, he enters at most n numbers that represent the lengths of the sides of the squares, and if he notices that there are fewer squares than the number of n, he enters −1, which indicates that the entry needs to be stopped.
The number n (0≤n≤15) is loaded from the standard input, followed by 1≤m≤n integers between 11 and 1010, where, if m<n, then −1 is entered after them.
Output
Print a single integer representing the total area of all the squares to the standard output.
Example 1
Input
5
1
2
3
4
5
Output
55
Example 2
Input
5
1
2
3
-1
Output
14
24. Savings in the bank
25. Monthly earning
26. Target shooting
27. The salt concentration
28. The number of k-element variations of n elements without repetition
29. The multiplication table
1 * 1 = 1
1 * 2 = 2
...
9 * 8 = 72
9 * 9 = 81
30. The Least Common Multiple
Previous
|< Selection statements - examples |
Next
Nested Loops - examples >| |