Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=========================================CLASSE PRINCIPAL===========================================//
- package bloco2;
- import java.util.Scanner;
- public class Bloco2 {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- Bloco b = new Bloco();
- System.out.println("O que deseja fazer?");
- System.out.println("1 - calcular fatorial");
- System.out.println("2 - verificar se é primo");
- System.out.println("3 - somar antecessores");
- System.out.println("4 - calcular o inteiro da divisão");
- System.out.println("5 - exibir números em forma de dígitos");
- int escolha = s.nextInt();
- switch(escolha){
- case 1:
- System.out.println("Digite o número para calcular o fatorial: ");
- int fatorial = s.nextInt();
- System.out.println(b.calculaFatorial(fatorial));
- break;
- case 2:
- System.out.println("Digite um número: ");
- int numero = s.nextInt();
- System.out.println(b.verificaPrimo(numero) ? "É primo!" : "Não é primo!");
- break;
- case 3:
- System.out.println("Digite um número: ");
- numero = s.nextInt();
- System.out.println("A soma de seus antecessores até 1 é: " + b.somaN(numero));
- break;
- case 4:
- System.out.println("Digite um número: ");
- int aa = s.nextInt();
- int bb = s.nextInt();
- System.out.println("O resto da divisão de "+aa+" e "+bb+" é: " + b.calculaResto(aa, bb));
- break;
- case 5:
- System.out.println("Digite um número: ");
- numero = s.nextInt();
- b.exibeSequencial(numero);
- break;
- default:
- System.out.println("Opção inválida, programa encerrado");
- }
- }
- }
- //=========================================CLASSE BLOCO===========================================//
- package bloco2;
- public class Bloco {
- public int calculaFatorial(int fatorial) {
- return (fatorial == 1 ? 1 : fatorial * calculaFatorial(fatorial - 1));
- /*
- System.out.println("Digite o número para calcular o fatorial: ");
- int fatorial = s.nextInt();
- System.out.println(b.calculaFatorial(fatorial));
- */
- }
- public boolean verificaPrimo(int numero) {
- int divisores = 0;
- for (int i = numero; i > 0; i--) {
- if (numero % i == 0) {
- divisores++;
- }
- }
- return (divisores == 2);
- /*
- System.out.println("Digite um número: ");
- int numero = s.nextInt();
- System.out.println(b.verificaPrimo(numero) ? "É primo!" : "Não é primo!");
- */
- }
- public int somaN(int numero) {
- int soma = 0;
- for (int i = 1; i <= numero; i++) {
- soma += i;
- }
- return soma;
- /*
- System.out.println("Digite um número: ");
- int numero = s.nextInt();
- System.out.println("A soma de seus antecessores até 1 é: " + b.somaN(numero));
- */
- }
- public int calculaResto(int a, int b) {
- return a % b;
- /*
- System.out.println("Digite um número: ");
- int aa = s.nextInt();
- int bb = s.nextInt();
- System.out.println("O resto da divisão de "+aa+" e "+bb+" é: " + b.calculaResto(aa, bb));
- */
- }
- public void exibeSequencial(int numero){
- int n1, n2, n3, n4, n5, n6;
- n1 = numero%10;// - (numero/10*10);
- n2 = (numero%100) / 10;// - (numero/100*10);
- n3 = (numero%1000) / 100;// - (numero/1000*10);
- n4 = (numero%10000) / 1000;// - (numero/10000*10);
- n5 = (numero%100000) / 10000;// - (numero/100000*10);
- n6 = (numero%1000000) / 100000;// - (numero/1000000*10);
- System.out.println(n6+" "+n5+" "+n4+" "+n3+" "+n2+" "+n1);
- /*
- System.out.println("Digite um número: ");
- int numero = s.nextInt();
- b.exibeSequencial(numero);
- */
- }
- /*
- public int exibeSequencia(int numero) {
- if(numero / 1000000 != 0){
- System.out.print(numero / 1000000+" ");
- numero %= 1000000;
- }else if(numero / 100000 != 0){
- System.out.print(numero / 100000+" ");
- numero %= 100000;
- }else if(numero / 10000 != 0){
- System.out.print(numero / 10000+" ");
- numero %= 10000;
- }else if(numero / 1000 != 0){
- System.out.print(numero / 1000+" ");
- numero %= 1000;
- }else if(numero / 100 != 0){
- System.out.print(numero / 100+" ");
- numero %= 100;
- }else if(numero / 10 != 0){
- System.out.print(numero / 10+" ");
- numero %= 10;
- }else{
- System.out.println(numero);
- return 0;
- }
- return exibeSequencia(numero);
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement