Home >Java >javaTutorial >List of Exercises to train Programming Logic.
I leave everyone in the community a list of exercises to train Programming Logic.
(I leave my resolution for each one, using the Java language)
Exercises
1 - Create an algorithm that reads the values of A, B, C and then prints the sum between A and B on the screen and shows whether the sum is less than C.
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Write a value of A:"); Scanner integerScanner = new Scanner(System.in); int A = Integer.parseInt(integerScanner.next()); System.out.println("Write a value of B:"); int B = Integer.parseInt(integerScanner.next()); System.out.println("Write a value of C:"); int C = Integer.parseInt(integerScanner.next()); int sum = A + B ; if (sum <= C){ System.out.println("The result of value A + B is: " + sum + ";\nThe sum of A + B is less or equal value C"); } else{ System.out.println("The result of value A + B is: " + sum + ";\nThe sum of A + B dont is less or equal value C"); } } }
2 - Create an algorithm to receive any number and print on the screen whether the number is even or odd, positive or negative.
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Write a number:"); Scanner integerScanner = new Scanner(System.in); int number = Integer.parseInt(integerScanner.next()); if (number >=0){ if (number % 2 == 0){ System.out.println("The number " + number + " is 'even' = par and positive"); } else System.out.println("The number " + number + " is 'odd' = impar and positive"); } else { if (number % 2 == 0){ System.out.println("The number " + number + " is 'even' = par and negative"); } else System.out.println("The number " + number + " is 'odd' = impar and negative"); } } }
3 - Make an algorithm that reads two integer values A and B, if the values of A and B are equal, you must add the two values,
otherwise you must multiply A by B. At the end of any of the calculations you must assign the result to a variable C and
print your value on the screen.
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Write your first value:"); Scanner integerScanner = new Scanner(System.in); int A = integerScanner.nextInt(); System.out.println("Write your second value:"); int B = integerScanner.nextInt(); int C; if (A == B){ C = A + B; System.out.println("The sum off A + B is: " + C); } else { C = (A * B); System.out.println("The multiple of A x B is: " + C); } } }
4 - Create an algorithm that receives an integer and prints its predecessor and successor on the screen.
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner integerScanner = new Scanner(System.in); System.out.println("Write your number: "); int number = integerScanner.nextInt(); int nextNumber = number + 1; int previousNumber = number - 1; System.out.println("The next number is: " + nextNumber); System.out.println("The previous number is: " + previousNumber); } }
5 - Create an algorithm that reads the value of the minimum wage and the value of a user's salary, calculates how many minimum wages this
user wins and prints the result on the screen. (Base for minimum wage R$ 1,293.20).
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner doubleScanner = new Scanner(System.in); System.out.println("Write your salario: "); double salarioMin = 1293.20; double salario = doubleScanner.nextDouble(); double total = salario / salarioMin; System.out.printf("the salario is: %.2fx o salario min" , total); } }
6 - Create an algorithm that reads any value and prints it on the screen with a 5% adjustment.
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Write a value: "); double a = scanner.nextDouble(); double total = a *.05; double result = a + total; System.out.printf("the value with 5%% increase is: %.2f" , result); } }
7 - Make an algorithm that reads two Boolean (logical) values and determines whether they are both TRUE or FALSE.
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Write a boolean value: "); boolean value1 = scanner.nextBoolean(); System.out.print("Write other boolean value: "); boolean value2 = scanner.nextBoolean(); if (value1 && value2){ System.out.println("values are true"); } else if (!value1 && !value2){ System.out.println("values are false"); } else { System.out.println("value are different"); } } }
8 - Make an algorithm that reads three different integer values and prints the values on the screen in descending order.
package org.example; import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Write your first value"); int value1 = scanner.nextInt(); System.out.println("Write your second value"); int value2 = scanner.nextInt(); System.out.println("Write your third value"); int value3 = scanner.nextInt(); int[] values = {value1, value2, value3}; Arrays.sort(values); System.out.println("Values in descending order:"); for (int i = values.length - 1; i >= 0; i--) { System.out.print(values[i] + " "); } } }
9 - Create an algorithm that calculates a person's BMI (Body Mass Index), reads their weight and height and prints their condition on the screen
according to the table below:
BMI formula = weight / (height) ²
BMI Conditions Table
Below 18.5 | Underweight
Between 18.6 and 24.9 | Ideal weight (congratulations)
Between 25.0 and 29.9 | Slightly overweight
Between 30.0 and 34.9 | Obesity grade I
Between 35.0 and 39.9 | Obesity grade II (severe)
Greater than or equal to 40 | Grade III obesity (morbid)
Em breve
10 - Create an algorithm that reads three grades obtained by a student, and prints the average of the grades on the screen.
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Write your first grade"); Scanner integerScanner = new Scanner(System.in); int grade = Integer.parseInt(integerScanner.next()); System.out.println("Write your second grade"); int grade2 = Integer.parseInt(integerScanner.next()); System.out.println("Write your third grade"); int grade3 = Integer.parseInt(integerScanner.next()); int sum = grade3 + grade + grade2; float result = (float)sum /3; System.out.printf("Your average grade is: %.1f" , result); } }
11 - Create an algorithm that reads four grades obtained by a student, calculates the average of the grades obtained, prints the student's name on the screen and
whether the student passed or failed. For the student to be considered approved, their final average must be greater than or equal to 7.
Em breve
12 - Create an algorithm that reads the value of a product and determines the amount that must be paid, depending on the choice of payment method
by the buyer and print the final value of the product to be paid on the screen. Use the codes in the payment terms table to make the appropriate calculation.
Payment Terms Code Table
1 - Cash or Pix, receive 15% discount
2 - Cash on credit card, receive 10% discount
3 - Paid in two installments on the card, normal price of the product without interest
4 - Installments on the card in three installments or more, normal price of the product plus 10% interest
Em breve
13 - Create an algorithm that reads the name and age of a person and prints the person's name on the screen and whether they are older or younger.
Em breve
14 - Make an algorithm that receives a value A and B, and exchanges the value of A for B and the value of B for A and prints the values on the screen.
Em breve
15 - Create an algorithm that reads the year a person was born, prints on the screen how many years, months and days that person has lived. Take it to
considering the year with 365 days and the month with 30 days.
(Ex: 5 years, 2 months and 15 days of life)
Em breve
16 - Make an algorithm that reads three values that represent the three sides of a triangle and checks if they are valid, determines if the triangle is
equilateral, isosceles or scalene.
Em breve
17 - Make an algorithm that reads a temperature in Fahrenheit and calculates the corresponding temperature in degrees Celsius. Print the two temperatures on the screen.
Em breve
Formula: C = (5 * (F-32) / 9)
18 - Francisco is 1.50m and grows 2 centimeters per year, while Sara is 1.10m and grows 3 centimeters per year. Create an algorithm that calculates and prints on the screen how many years it will take for Francisco to be bigger than Sara.
Em breve
19 - Create an algorithm that prints the multiplication table from 1 to 10 on the screen.
Em breve
20 - Create an algorithm that receives an integer value and prints its multiplication table on the screen.
Em breve
21 - Faça um algoritmo que mostre um valor aleatório entre 0 e 100.
Em breve
22 - Faça um algoritmo que leia dois valores inteiros A e B, imprima na tela o quociente e o resto da divisão inteira entre eles.
Em breve
21 - Faça um algoritmo que efetue o cálculo do salário líquido de um professor. As informações fornecidas serão: valor da hora aula, número de aulas lecionadas no mês e percentual de desconto do INSS. Imprima na tela o salário líquido final.
Em breve
22 - Faça um algoritmo que calcule a quantidade de litros de combustível gastos em uma viagem, sabendo que o carro faz 12km com um litro. Deve-se fornecer ao usuário o tempo que será gasto na viagem a sua velocidade média, distância percorrida e a quantidade de litros utilizados para fazer a viagem.
Fórmula: distância = tempo x velocidade.
litros usados = distância / 12.
Em breve
Créditos:
Todos os exercícios da lista acima foram obtidos da DIO.
Link: https://www.dio.me/articles/lista-de-exercicios-para-treinar-logica-de-programacao
The above is the detailed content of List of Exercises to train Programming Logic.. For more information, please follow other related articles on the PHP Chinese website!