Last active
October 20, 2017 19:13
-
-
Save diogocapela/35594368c135a1ea08ef45c2f6bc840b to your computer and use it in GitHub Desktop.
APROG - PL05 Implementação de Algoritmos em Java e Decomposição Modular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
public class Main { | |
public static void main(String[] args) { | |
int n; | |
int num; | |
int totalPares = 0; | |
int somaPares = 0; | |
double mediaPares; | |
double percentagemPares; | |
String nString = JOptionPane.showInputDialog("Introduza a quantidade de números que deseja ler."); | |
n = Integer.parseInt(nString); | |
for(int i = 1; i <= n; i++) { | |
String numString = JOptionPane.showInputDialog("Introduza um número"); | |
num = Integer.parseInt(numString); | |
if(num % 2 == 0) { | |
totalPares++; | |
somaPares = somaPares + num; | |
} | |
} | |
if(totalPares != 0) { | |
mediaPares = somaPares / totalPares; | |
percentagemPares = (totalPares / n) * 100; | |
JOptionPane.showMessageDialog(null, "Media dos números pares: " + mediaPares); | |
JOptionPane.showMessageDialog(null, "Percentagem dos números pares: " + percentagemPares); | |
} else { | |
JOptionPane.showMessageDialog(null, "Não existem números pares."); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
public class Main { | |
public static void main(String[] args) { | |
int dias; | |
int tempMaxima; | |
String diasString = JOptionPane.showInputDialog("Introduza o número de dias."); | |
dias = Integer.parseInt(diasString); | |
for(int i = 1; i <= dias; i++) { | |
String tempMaximaString = JOptionPane.showInputDialog("Introduza a temperatura máxima do dia " + i + "."); | |
tempMaxima = Integer.parseInt(tempMaximaString); | |
if(tempMaxima >= 50) { | |
JOptionPane.showMessageDialog(null, "Dia " + i + ": Temperatura Extrema"); | |
} else if(tempMaxima >= 30) { | |
JOptionPane.showMessageDialog(null, "Dia " + i + ": Muito Quente"); | |
} else if(tempMaxima >= 20) { | |
JOptionPane.showMessageDialog(null, "Dia " + i + ": Quente"); | |
} else if(tempMaxima >= 15) { | |
JOptionPane.showMessageDialog(null, "Dia " + i + ": Ameno"); | |
} else if(tempMaxima >= 9) { | |
JOptionPane.showMessageDialog(null, "Dia " + i + ": Frio"); | |
} else if(tempMaxima >= -30) { | |
JOptionPane.showMessageDialog(null, "Dia " + i + ": Muito Frio"); | |
} else { | |
JOptionPane.showMessageDialog(null, "Dia " + i + ": Temperatura Extrema"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
public class Main { | |
public static void main(String[] args) { | |
String nome; | |
int idade; | |
int totalPessoas = 0; | |
int totalMaioresIdade = 0; | |
int percentagemMaioresIdade; | |
do { | |
nome = JOptionPane.showInputDialog("Intorduza o nome da pessoa."); | |
if(!"zzz".equals(nome)) { | |
String idadeString = JOptionPane.showInputDialog("Introduza a idade do/a " + nome + "."); | |
idade = Integer.parseInt(idadeString); | |
totalPessoas++; | |
if(idade >= 18) { | |
totalMaioresIdade++; | |
} | |
} | |
} while (!"zzz".equals(nome)); | |
percentagemMaioresIdade = (totalMaioresIdade / totalPessoas) * 100; | |
JOptionPane.showMessageDialog(null, "Percentagem de pessoas com idade maior ou igual a 18 anos: " + percentagemMaioresIdade); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
int n; | |
Scanner scanner = new Scanner(System.in); | |
do { | |
System.out.println("Introduza N:"); | |
n = scanner.nextInt(); | |
} while(n <= 0); | |
for(int i = 1; i <= n; i++) { | |
int somaDivisores = 0; | |
for(int k = 1; k < i; k++) { | |
if(i % k == 0) { | |
somaDivisores = somaDivisores + i; | |
} | |
} | |
if(i == somaDivisores) { | |
System.out.println("Número perfeito encontrado: " + i); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
int num1; | |
int num2; | |
int numerosPares1 = 0; | |
int numerosPares2 = 0; | |
Scanner scanner = new Scanner(System.in); | |
do { | |
System.out.println("Introduza um número para a primeira sequência:"); | |
num1 = scanner.nextInt(); | |
if(num1 % 2 == 0) { | |
numerosPares1++; | |
} | |
} while(num1 != 0); | |
do { | |
System.out.println("Introduza um número para a segunda sequência:"); | |
num2 = scanner.nextInt(); | |
if(num2 % 2 == 0) { | |
numerosPares2++; | |
} | |
} while(num2 != -1); | |
if(numerosPares1 == numerosPares2) { | |
System.out.println("As sequências têm a mesma quantidade de números pares."); | |
} else if (numerosPares1 > numerosPares2) { | |
System.out.println("A primeira sequência é a que tem mais quantidade de números pares."); | |
} else { | |
System.out.println("A segunda sequência é a que tem mais quantidade de números pares."); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
int num; | |
boolean binario = false; | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Introduza um número:"); | |
num = scanner.nextInt(); | |
while(num != 0) { | |
if(num % 10 > 1) { | |
binario = false; | |
} else { | |
binario = true; | |
} | |
num = num / 10; | |
} | |
if(binario) { | |
System.out.println("O número introduzido é binário."); | |
} else { | |
System.out.println("O número introduzido não é binário."); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
public class Main { | |
public static void main(String[] args) { | |
int binario; | |
double decimal = 0; | |
int ordem = 0; | |
int alg; | |
String binarioString = JOptionPane.showInputDialog("Introduza um número em binário:"); | |
binario = Integer.parseInt(binarioString); | |
while(binario > 0) { | |
alg = binario % 10; | |
decimal = decimal + alg * Math.pow(2,ordem); | |
binario = binario / 10; | |
ordem++; | |
} | |
JOptionPane.showMessageDialog(null,"O número em base decimal é: " + decimal); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
int num; | |
int n1; | |
int n2; | |
int persistencia = 0; | |
int alg; | |
boolean acabar = false; | |
Scanner scanner = new Scanner(System.in); | |
do { | |
System.out.println("Introduza um número:"); | |
num = scanner.nextInt(); | |
} while(num <= 0); | |
n1 = num; | |
do { | |
System.out.print(n1); | |
n2 = n1; | |
n1 = 1; | |
while(n2 > 0) { | |
alg = n2 % 10; | |
n1 = n1 * alg; | |
n2 = n2 / 10; | |
} | |
System.out.print(" | Persistência: " + persistencia); | |
persistencia++; | |
System.out.println(); | |
if(n1 == 0) { | |
System.out.println("0 | Persistência: " + persistencia); | |
persistencia++; | |
acabar = true; | |
} | |
} while(!acabar); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
int n; | |
int seq1 = 0; | |
int seq2 = 1; | |
int seq3; | |
Scanner scanner = new Scanner(System.in); | |
do { | |
System.out.println("Sucessão de Fibonacci até qual número?"); | |
n = scanner.nextInt(); | |
} while(n <= 0); | |
System.out.println("Sucessão de Fibonacci até " + n + ":"); | |
for(int i = 2 ; i < n; i++) { | |
seq3 = seq1 + seq2; | |
System.out.print(" " + seq3); | |
seq1 = seq2; | |
seq2 = seq3; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment