JetBrains Academy: The for loop - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: The for loop

Lucky number:

Given the number N with an even number of digits. If the sum of the first half of the digits equals the sum of the second half of the digits, then this number is considered lucky. For a given number, output "YES" if this number is lucky, otherwise output "NO".

import java.util.*;

public class LuckyNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        String n = scanner.nextLine();
        int sumX = 0;
        int sumY = 0;

        if(n.length() % 2 == 0){
            for(int i = 0; i < n.length(); i++){
                if (i < n.length()/2){
                    int x = Character.getNumericValue(n.charAt(i));
                    sumX += x;
                } else {
                    int y = Character.getNumericValue(n.charAt(i));
                    sumY += y;
                }
            }
            if (sumX == sumY){
                System.out.println("YES");
            } else{
                System.out.println("NO");
            }
        } else {
            System.out.println("Odd number of digits");
        }
    }
}

Arithmetic average:

Write a program that reads two numbers a and b from the keyboard and calculates and outputs to the console the arithmetic average of all numbers from the interval [a; b], which are divisible by 3.

For example, the arithmetic average is calculated for the numbers on the interval [-5; 12]. Total numbers divisible by 3 on this interval is 6: -3, 0, 3, 6, 9, 12. Their arithmetic average equals to 4.5

The program input contains intervals, which always contain at least one number, which is divisible by 3.

import java.util.*;

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

        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int counter = 0;
        double sum = 0.0;
        double average = 0.0;

        for (int i = a; i <= b; i++){
            if(i % 3 == 0){
                sum += i;
                counter++;
            }
        }
        average = sum / counter;
        System.out.println(average);
    }
}

Fizz Buzz:

Fizz Buzz is a classic programming problem. Here is its slightly modified version.

Write a program that takes the input of two integers: the beginning and the end of the interval (both numbers belong to the interval).

The program should output the numbers from this interval, but if the number is divisible by 3, you should output Fizz instead of it; if the number is divisible by 5, output Buzz; and if it is divisible both by 3 and by 5, output FizzBuzz.

Output each number or the word on a separate line.

import java.util.*;

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

        int a = scanner.nextInt();
        int b = scanner.nextInt();

        for (int i = a; i <= b; i++){
            if (i % 3 == 0 && i % 5 == 0){
                System.out.println("FizzBuzz");
            } else if (i % 3 == 0){
                System.out.println("Fizz");
            } else if (i % 5 == 0){
                System.out.println("Buzz");
            } else {
                System.out.println(i);
            }
        }
    }
}

Grades:

Find the number of "Ds", "Cs", "Bs" and "As" for the last test on informatics in a class consisting of n students. The program gets number n as input, and then gets the grades themselves (one by one). The program should output four numbers in a single line - the number of "D", the number of "C", the number of "B" and the number of "A" grades.

import java.util.*;

class Grades {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int numberOfStudents = scanner.nextInt();
        int amountOfAs = 0;
        int amountOfBs = 0;
        int amountOfCs = 0;
        int amountOfDs = 0;

        for(int i = 0; i < numberOfStudents; i++){
            int x = scanner.nextInt();
            if (x == 5){
                amountOfAs++;
            } else if (x == 4){
                amountOfBs++;
            } else if (x == 3){
                amountOfCs++;
            } else if (x == 2){
                amountOfDs++;
            }
        }
        System.out.printf("%d %d %d %d", amountOfDs, amountOfCs, amountOfBs, amountOfAs);
    }
}

Maximum element divisible by 4:

Given a sequence of natural numbers, not exceeding 30000. Find the maximum element divisible by 4. The program receives the number of elements in the sequence and then the elements themselves as input. 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 DivisibleBy4Max {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
	    int[] numbers = new int[size];

	    for (int i = 0; i < size; i++){
	        numbers[i] = scanner.nextInt();
        }
        
        Arrays.sort(numbers);

        for (int j = size-1; j >=0; j--){
            if(numbers[j] % 4 == 0){
                System.out.println(numbers[j]);
		        break;
            }
        }
    }
}

Size of parts:

A detector compares the size of parts produced by a machine with the reference standard.

If the size of the part is larger, it can be sent to be fixed, and the detector prints the number 1. If the size of the part is smaller, it is removed as a reject, and the detector prints the number -1. If the part is perfect, it is sent to the box with products, that are ready to ship, and the detector prints 0.

Write a program, which takes the number of parts n as input, and then the sequence of detector prints. The program should output numbers in a single line containing the number of parts ready to be shipped, the number of parts to be fixed, and the number of rejects.

import java.util.Scanner;

class Parts {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int perfect = 0;
        int fix = 0;
        int reject = 0;
        int n = scanner.nextInt();

        for (int i = 0; i < n; i++){
            int detector = scanner.nextInt();

            if (detector == 0) {
                perfect++;
            } else if (detector > 0) {
                fix++;
            } else {
                reject++;
            }
        }
        System.out.println(perfect + " " + fix + " " + reject);
    }
}

Sum of numbers divisible by 6:

Given the sequence of natural numbers. Find the sum of numbers divisible by 6. The input is the number of elements in the sequence, and then the elements themselves. In this sequence, there is always a number divisible by 6.

import java.util.*;

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

        int numberOfElements = scanner.nextInt();
        int sum = 0;

        for (int i = 0; i < numberOfElements; i++){
            int element = scanner.nextInt();
            if(element % 6 == 0){
                sum += element;
            }
        }
        System.out.println(sum);
    }
}

The count of numbers divisible by N:

Write a program that reads a, b, n and outputs the count of numbers divisible by n in the range from a to b (a < b) inclusively.

import java.util.Scanner;

class DivisibleByN {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int lowerBoundary = scanner.nextInt();
        int upperBoundary = scanner.nextInt();
        int divider = scanner.nextInt();
        int dividableNumbers = 0;

        for (int i = lowerBoundary; i <= upperBoundary; i++){
            dividableNumbers += (i % divider == 0) ? 1 : 0;
        }
        System.out.println(dividableNumbers);
    }
}

The product of numbers from a to b:

Write a program that prints the product of all integer numbers from a to b (a < b). Include a and exclude b from the product.

import java.util.*;

class ProductOf {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long product = 1;
        int lowerBoundary = scanner.nextInt();
        int upperBoundary = scanner.nextInt();

        for (int i = lowerBoundary; i < upperBoundary; i++){
            product *= i;
        }
        System.out.println(product);
    }
}

The roots of an equation:

Given the numbers a, b, c, d. Output in ascending order all the integers between 0 and 1000 which are the roots of the equation ax^3 + bx^2 + cx + d = 0.

If the specified interval does not contain the roots of the equation, do not output anything.

import java.util.*;

class Roots {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int d = scanner.nextInt();

        int x;

        for ( x = 0; x < 1000; x++){
            if ( a * x * x * x + b * x * x + c * x + d == 0){
                System.out.println(x);
            }
        }
    }
}

The sum of integers from a to b:

Print the sum of all integers from a to b including both.

It is guaranteed that a < b in all test cases.

import java.util.*;

class Sum {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int sum = 0;

        for (int i = a; i <= b; i++){
            sum += i;
        }
        System.out.println(sum);
    }
}