JetBrains Academy: The while & do while loops - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: The while & do-while loops

Collatz conjecture:

Given natural number n. Generate a sequence of integers, described in the Collatz conjecture:

If n is an even number, divide it in half, if it is odd, multiply it by 3 and add 1. Repeat this operation until we get the number 1 as a result.

import java.util.*;

class CollatzConjecture {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        while (n>0){
            System.out.println(n);
            if(n % 2 == 0 && n != 1){
                n = n/2;
            } else if (n % 2 != 0 && n != 1){
                n = n * 3 + 1;
            } else {
                n--;
            }
        }
    }
}

The largest element of a sequence:

Given the sequence of integer numbers (which ends with the number 0). Find the largest element of the sequence.

The number 0 itself is not included in the sequence but serves only as a sign of the sequence’s end.

import java.util.Scanner;

class Largest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        int currentMax = 0;

        while (input != 0){
            if (input > currentMax){
                currentMax = input;
            }
            input = scanner.nextInt();
        }
        System.out.println(currentMax);
    }
}

Profit:

Ann put M money in the bank. The bank increases Ann's deposit by P percent every year. Ann wants to know how many years should pass until her deposit in the bank reaches K money. Can you help her to answer this question?

Input contains three integers - M, P, K. It is guaranteed that all numbers are positive and K ≥ M. Output the answer to Ann's question.

import java.util.*;

public class Profit {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double money = scanner.nextInt();
        double percentage = (double) scanner.nextInt()/100;
        int savingsGoal = scanner.nextInt();
        int yearsOfSaving = 0;

        while (money < savingsGoal){
            money += money*percentage;
            yearsOfSaving++;
        }
        System.out.println(yearsOfSaving);
    }
}

Squares of natural numbers:

Given integer N. Print all the squares of natural numbers, not exceeding N, in ascending order.

import java.util.*;

class Squares {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();

        for (int i = 0; i <= number; i++){
            double j = 0;
            while (j < i){
                j++;
                if (i / j == j){
                    System.out.println(i);
                }
            }
        }
    }
}

The length of the sequence:

For its input, the program gets a sequence of non-negative integers; each integer is written in a separate line. The sequence ends with an integer 0; when the program reads 0, it should end its work and output the length of the sequence (not counting the final 0).

Don’t read numbers following the number 0.

import java.util.*;

class LengthOfSequence {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = 0;
        int count = 0;
        do {
            input = scanner.nextInt();
            count++;
        } while(input != 0);
        System.out.println(count-1);
    }
}

The sequence -1:

Given a sequence of natural numbers, not exceeding 30000. Find the maximum element divisible by 4. As input, the program gets the number of elements in the sequence, and then the elements themselves. In the sequence, there is always an element divisible by 4. The number of elements does not exceed 1000. The program should print a single number: the maximum element of the sequence divisible by 4.

import java.util.*;

class DivisibleBy4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int numberOfElements = scanner.nextInt();
        int maxDivisibleBy4 = 0;
        int i = 0;
        while (i < numberOfElements){
            int element = scanner.nextInt();
            if (element % 4 == 0){
                if (element > maxDivisibleBy4){
                    maxDivisibleBy4 = element;
                }
            }
            i++;
        }
        System.out.println(maxDivisibleBy4);
    }
}

The smallest value:

A user inputs a long number M. You need to find out what is the smallest long number n such that n! > M.

import java.util.*;

class Factorial {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        long input = scanner.nextLong();
        long factorial = 1;
	    long n = 0;

 	    while (factorial <= input){
            n++;
	        factorial *= n;
        }
        System.out.println(n);
    }
}

The sum of elements:

Find the sum of all elements of a sequence, ending with the number 0.

The number 0 itself is not included into the sequence and serves as a sign of cessation.

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int element = scanner.nextInt();
        int sum = element;

        while (element != 0){
            element = scanner.nextInt();
            sum += element;
        }
        System.out.println(sum);
    }
}