additional examples from the loop topic
1. Printing numbers
The numbers that are divisible by 4 and starting from -50 are: -48,-44,-40,-36,...0,4,8,...88
Let's observe the following: The first number in the sequence is -48, every 4th starting from that number is also divisible by 4
Use a for loop and set the control variable "i" to change, so that the initial value is -48, and through the cycles it increases by 4 each time. Print the value i inside the for loop.
#include < iostream >
using namespace std ;
int main()
{
for (int i = -48; i<90; i = i + 4)
{
cout << i << " ";
}
return 0;
}
package writing.numbers.divisible.by.pkg4;
public class WritingNumbersDivisibleBy4 {
public static void main(String[] args) {
for (int i = -48; i < 90; i = i + 4) {
System.out.print(i + " ");
2. Writing numbers backwards from some given number N
The task is solved using loops
N is loaded first
The initial value of the control variable "i" is set equal to N
Use a for loop and set the control variable "i" to change, so that the initial value is N, and it decreases by 1 through the cycles. For the condition, set the condition that the values are positive.
Inside the loop, check if "i" is an odd number (if). If so, print "i"
The task can be solved in another way. To first check whether N is odd. If not reduce by 1 to get the first smaller odd number. Then put that number for the initial value of "i". As in the first mode, "i" is decremented by two during the cycle, so all values of "i" will be odd. Therefore, inside the body of the loop there is no need to check whether "i" is an odd number, so there is no if statement.
#include < iostream >
using namespace std ;
int main()
{
int N;
cin >> N;
//"i" is changed starting at N and decremented by 2 each cycle
for (int i = N; i > 0; i--)
{
//If the number is odd
if( i % 2 != 0){
cout << i << " ";
}
}
return 0;
}
3. Calculating the sum of entered numbers
Example:
Input
4
3
5
6
7
Output:
21
Explanation: The first number loaded is 4. That's how many numbers need to be loaded and added. So sum = 3+5+6+7.
The task is solved by using for loops. We initialize the control variable "i" to 1, as a condition set i to be less than N, and for the change step "i" the value 1. In this way, N cycles are provided
We create a collection variable and give it an initial value of zero. Do this before for commands
Inside the cycle, write a formula that should add a newly loaded value to the previously formed sum, something like sum=sum + number
After exiting the loop, print the calculated sum.
#include < iostream >
#include < ios >
using namespace std ;
int main()
{
int x,N;
int sum=0;//Declare variable sum, and initialize it on zero
cin >> N;
//for loop with N cycles is created
for(int i = 1; i <= N; i++)
{
cin >> x;//Enter new number in the loop
sum = sum + x;//Add previously entered number in the sum.
}
cout << sum << endl;//print the sum return 0;
}
4. Determination of the maximum
Example:
Input
4
-2
8
2
3
Output:
8
The task is solved using a for loop. We initialize the control variable "i" to 1, as a condition set i to be less than N, and for the change step "i" the value 1. In this way, N cycles are provided
We create the maximum variable
Inside the loop we first load the number x
Next, within the cycle, let's check if it is the first cycle (when i=1)
If so, let's set the maximum to the value of the first loaded number x
We perform one more check in the body of the cycle and that is whether the newly loaded number is greater than the current maximum
If so, let's set the maximum to the value of the new loaded number x. If not, it goes to the next cycle without changing the maximum
In the first cycle, this check certainly does not pass because the newly loaded number x and the current maximum are the same, ie. equal to x
2 way
Another way to solve it would be to set the maximum number before the for loop, which will certainly be smaller than all the loaded numbers. For example, if we know that the possible numbers are from -100 to 100, then we could set the initial maximum to -100
Within the for cycle, there would then be only another check, i.e. whether the loaded number is greater than the current maximum and if so the maximum value would be changed to be equal to the newly loaded number
#include < iostream >
#include < ios >
using namespace std ;
int main()
{
int x,N;
int max;
cin >> N;
//It create for loop with N cycles
for(int i = 1; i <= N; i++)
{
cin >> x;//On every cycles, new number has been entered
//If is the first cycle
if(i == 1)
max = x;//Entered number is maximum
//In the every followed cycle it is check
// if the new number is greather than current maximum
if(x > max){
max=x;//If so, it will be new maximum
}
}
cout << max << endl;//Printing maximum return 0;
}
5. Forming the sum of numbers through the loop
The task is solved by using a do-while loop because we have to achieve at least one loading, ctrl + Z, the character with which we stop loading
we create a variable sum and give it an initial value of zero
Inside the cycle, write a formula that should add a newly loaded value to the previously formed sum, something like sum=sum + number
Inside the loop, this formula must be under an if statement, to check the condition if the character loaded is different from "ctrl + z"
This should also be the condition of the do-while loop
#include < iostream >
#include < ios >
using namespace std ;
int main()
{
int sum = 0;
int x;
do
{
cin >> x; //Enter new number
//if ctrl+Z character isn't entered
if(!cin.eof())
sum=sum+x; //Add entered number into sum
}
while(!cin.eof()); /*if ctrl+Z character is entered
go to next cycle*/
cout << sum << endl;
return 0;
}
package sabiranjebrojevaucitanihsatastature;
import java.util.Scanner;
public class SummingTheNumbersReadFromTheKeyboard {
public static void main(String[] args) {
int a, s = 0;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("Enter the number");
a = scanner.nextInt();
s = s + a;
} while (ucitavac.hasNextInt());
System.out.println("s= " + s);
}
}
6. Geometric series of numbers
Input
Natural numbers a (1≤a≤50) and b (a≤b≤10000 ) are loaded from the standard input, each in a separate row.
Output
All requested numbers are printed on the standard output, in order (from smallest to largest). Write each number on a separate line.
Example
Input
5
50
Output
5
15
45
We load the limits of intervals a and b. We introduce a variable e.g. x and we initialize it so that it is initially equal to the first natural number a. In the loop, in each iteration, this number is multiplied by 3: x=3*x. Since we do not know in advance how many cycles there will be, it is better to use a while loop here than a for. The condition is that x is less than b. Inside the loop, print the obtained x before it is multiplied by 3.
#include < iostream >
using namespace std ;
int main()
{
int a,b;
cin >> a >> b;
int x=a;
while(x<=b)
{
cout << x << endl;
x=x*3;
}
return 0;
}
7. Dividing the interval into equal parts
Input
The first line of the standard input contains the natural number n (1<n≤20), the second line contains the real number a, and the third line the real number b, where a<b.
Output
On the standard output, display the requested numbers in sequence, each in a separate line, to five decimal places.
Example
Input
5
-1
1
Output
-1.00000
-0.50000
0.00000
0.50000
1.00000
We load the number of real numbers n, as well as the limits of the intervals a and b. Let's introduce a variable, e.g. k and we initialize it to be equal to the first real number a. We calculate the distance between adjacent numbers: d=(b-a)/(n-1).
In the loop, in each iteration, the current number is added with the calculated fixed difference of two adjacent numbers d. Since it is known in advance how many cycles there will be, i.e. that value was calculated previously, it is more convenient to use here for a loop. Inside the loop, the number k is printed and the value of k is calculated for the next iteration.
#include < iostream >
#include < iomanip>
using namespace std ;
int main()
{
int n;
double a,b,x;
cin >> n >> a >> b;
double d=(b-a)/(n-1);
x=a;
for(int i=1; i<=n; i++)
{
cout << fixed << setprecision(5) << x << endl;
x=x+d;
}
return 0;
}
8. Maximum and minimum temperature
- maximum and minimum temperature in that period
- consecutive number of days when it was maximum and consecutive number of days when it was minimum temperature
- how many days in that period the maximum temperature was reached
The first line of the standard input contains a natural number n (1<n≤20), the other n lines contain numbers representing temperatures for each of the N days of the year.
Output
On the standard output, display the maximum temperature, the minimum temperature, the consecutive number of days when the maximum temperature was reached, the consecutive number of days when the minimum temperature was reached, how many days in that period the maximum temperature was reached, each number in a separate line.
Example
Input
8
19 19 21 23 12 23 12 11
Output
23
11
4
8
2
We enter the number of days of temperature reading N, and then using a for loop we load the temperatures. To determine the maximum, before entering the loop we initialize the initial value for the maximum temperature to some large value, which is certainly greater than the actual maximum, e.g. to 1000. Inside the for loop, using the if command, we examine whether the previously loaded temperature in some current cycle may be higher than the currently remembered maximum. If so, then a new maximum should be set to that value. This should be done in the body of the if statement. The ordinal number of days of the maximum value is also remembered in the body of the if statement, i.e. when the condition that the current temperature is higher than the previous maximum is met. The minimum temperature should be determined in a similar way. As an initial value, you should now set a temperature that is certainly lower than the minimum. If the loaded temperature is lower than the currently memorized minimum, then the new lowest minimum value should be memorized as well as the consecutive number of days. Counting the maximum again should be done if the condition that the loaded temperature is equal to the current maximum is satisfied. And then the integer variable representing the number of maximum temperatures should be increased by 1.
#include < iostream >
using namespace std ;
int main()
{
int N,rbMax=0,rbMin=0,br=0;
double T,minT=1000,maxT=-273;//We suppose that maximum is the lowest posible temperature
// and the minimum is the highest or some temperature that is higher than the maximum
cin >> N;
for(int i = 0;i < N; i++){
cin >> T;
if(T > maxT){//If the entered temperature is higher than the current maximum, it becomes the maximum
maxT=T;
rbMax=i+1;//it remembers the consecutive number of days when this maximum temperature was reached
br=1;
}
else if(T == maxT){//This means that the maximum value is repeated and the number of maximum temperatures is increased by 1
br++;
}
if(T < minT){
minT=T;
rbMin=i+1;
}
}
cout << maxT << endl;
cout << minT << endl;
cout << rbMax << endl;
cout << rbMin << endl;
cout << br << endl;
return 0;
}
9. TASK: THE SEQUENCE NUMBER OF THE MAXIMUM DISTANCE
If there are several distances with the same maximum, print the sequence number of the last occurrence of that maximum.
Example
Input
8
45 23 12 56 32 56 18 2
Output
6