DEV Community

Cover image for Lista de Exercícios para treinar Lógica de Programação 2.
Pedro Borges Dev
Pedro Borges Dev

Posted on • Edited on

Lista de Exercícios para treinar Lógica de Programação 2.

Deixo para todos da comunidade uma lista de exercícios para treinar a Lógica de Programação.
(deixo minha resolução de cada um, usando a linguagem Java)

Exercícios

1- Encontre o maior número em uma lista: Escreva um algoritmo para encontrar o maior número em uma lista de valores.

public class Main {

    public static void main(String[] args) {
        int[] num = new int[] {1,2,3,5,3,2,13,13,13,5,12};
        int high = num[0];

        for(int i = 0; i < num.length; i++){
            if(num[i] > high){
                high = num[i];
            }
       } System.out.println(high);
}
}
Enter fullscreen mode Exit fullscreen mode

2- Verifique se um número é primo: Crie um programa que determine se um número é primo ou não.

Em breve
Enter fullscreen mode Exit fullscreen mode

3- Inverta uma string: Desenvolva um algoritmo que inverta uma string fornecida como entrada.

package org.example;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.println("Escreva uma palavra");
        Scanner scan = new Scanner(System.in);
        String word = scan.next();
        String reverse = new StringBuilder(word).reverse().toString();
        System.out.println("Sua palavra invertida eh: " + reverse);
    }
}
Enter fullscreen mode Exit fullscreen mode

4- Calcule o fatorial de um número: Escreva um programa que calcule o fatorial de um número dado.

package org.example;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.println("Write a number");
        Scanner scan = new Scanner(System.in);
        int number = scan.nextInt();
        if (number < 0) {
            System.out.println("Invalid number");
        } else if (number == 0) {
            System.out.println("The factorial of 0 is 1");
        } else {
            int factorial = 1;
            for (int i = 1; i < number; i++){
                factorial +=  factorial * (number - i);
            }
            System.out.println("The factorial of " + number + " is " + factorial);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

5- Ordenação de lista: Implemente um algoritmo de ordenação para ordenar uma lista de valores.

Em breve
Enter fullscreen mode Exit fullscreen mode

6- Verifique se uma palavra é um palíndromo: Crie um programa que identifique se uma palavra é um palíndromo.

package org.example;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.println("Escreva uma palavra ou frase");
        Scanner scan = new Scanner(System.in);
        String word = scan.next();
        String reverse = new StringBuilder(word).reverse().toString();
        if(reverse.equals(word)){
            System.out.println("eh palindromo");
        } else {
            System.out.println("nao eh palindromo");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

7- Verifique se dois conjuntos são iguais: Desenvolva um algoritmo para
verificar se dois conjuntos possuem os mesmos elementos.

package org.example;
import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] conjunto1 = new int[5];
        System.out.println("digite " + conjunto1.length + " numeros: ");
        for (int i = 0; i < conjunto1.length; i++) {
            conjunto1[i] = scan.nextInt();
        }
        int[] conjunto2 = new int[] {1,2,3,4,5};

        if (!Arrays.equals(conjunto1, conjunto2)){
            System.out.println("Os conjuntos nao possuem o mesmos elementos");
        } else {
            System.out.println("Os conjuntos possuem os mesmos elementos");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

8- Encontre o número que falta: Dada uma sequência de números de 1 a N, encontre o número que está faltando.

Em breve
Enter fullscreen mode Exit fullscreen mode

9- Encontre o menor número em uma lista: Escreva um programa para encontrar o menor número em uma lista de valores.

package org.example;
public class Main {
    public static void main(String[] args) {
        int[] listOfNumbers = new int[]{7, 7, 7, 7, 7, 4, 7, 8, 9, 10};
    int min = listOfNumbers[0];
    for (int i = 1; i < listOfNumbers.length; i++){
        if (listOfNumbers[i] < min){
            min = listOfNumbers[i];
        }
    }
    System.out.println("O menor número é: " + min
    );
    }
}
Enter fullscreen mode Exit fullscreen mode

10- Converta um número decimal para binário: Implemente um algoritmo que converta um número decimal para seu equivalente em binário.

Em breve
Enter fullscreen mode Exit fullscreen mode

11- Digite um numero para retornar uma letra do alfabeto
exemplo: input 3 retorna, a, b,c

package org.example;
import java.util.Scanner;
//0 a 26 - A a Z
// digitei 2 eh pra retornar A e B
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Digite um numero para retornar uma letra do alfabeto");
        int num = scan.nextInt();
        char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();

        if (num > 0 && num <= 26){
            for (int i = 0; i < num; i++) {
                System.out.println(alphabet[i]);
            }
           //System.out.println("A letra na posicao " + num + " eh " + alphabet[num - 1]); aqui fiz uma graca a +
        } else {
            System.out.println("Digite um numero entre 1 e 26");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Créditos:
Todos os exercícios da lista acima foram obtidos da Awari.
Link: https://awari.com.br/10-exercicios-de-logica-de-programacao-para-aprimorar-suas-habilidades/

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay