Advertisement
Guest User

Bloco2

a guest
Apr 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.15 KB | None | 0 0
  1. //=========================================CLASSE PRINCIPAL===========================================//
  2. package bloco2;
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class Bloco2 {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner s = new Scanner(System.in);
  10.         Bloco b = new Bloco();
  11.        
  12.         System.out.println("O que deseja fazer?");
  13.         System.out.println("1 - calcular fatorial");
  14.         System.out.println("2 - verificar se é primo");
  15.         System.out.println("3 - somar antecessores");
  16.         System.out.println("4 - calcular o inteiro da divisão");
  17.         System.out.println("5 - exibir números em forma de dígitos");
  18.         int escolha = s.nextInt();
  19.         switch(escolha){
  20.             case 1:
  21.                 System.out.println("Digite o número para calcular o fatorial: ");
  22.                 int fatorial = s.nextInt();
  23.                 System.out.println(b.calculaFatorial(fatorial));
  24.                 break;
  25.             case 2:
  26.                 System.out.println("Digite um número: ");
  27.                 int numero = s.nextInt();
  28.                 System.out.println(b.verificaPrimo(numero) ? "É primo!" : "Não é primo!");
  29.                 break;
  30.             case 3:
  31.                 System.out.println("Digite um número: ");
  32.                 numero = s.nextInt();
  33.                 System.out.println("A soma de seus antecessores até 1 é: " + b.somaN(numero));
  34.                 break;
  35.             case 4:
  36.                 System.out.println("Digite um número: ");
  37.                 int aa = s.nextInt();
  38.                 int bb = s.nextInt();
  39.                 System.out.println("O resto da divisão de "+aa+" e "+bb+" é: " + b.calculaResto(aa, bb));
  40.                 break;
  41.             case 5:
  42.                 System.out.println("Digite um número: ");
  43.                 numero = s.nextInt();
  44.                 b.exibeSequencial(numero);
  45.                 break;
  46.             default:
  47.                 System.out.println("Opção inválida, programa encerrado");
  48.         }
  49.        
  50.     }
  51.    
  52. }
  53.  
  54. //=========================================CLASSE BLOCO===========================================//
  55. package bloco2;
  56.  
  57. public class Bloco {
  58.  
  59.     public int calculaFatorial(int fatorial) {
  60.         return (fatorial == 1 ? 1 : fatorial * calculaFatorial(fatorial - 1));
  61.         /*
  62.         System.out.println("Digite o número para calcular o fatorial: ");
  63.         int fatorial = s.nextInt();
  64.         System.out.println(b.calculaFatorial(fatorial));
  65.          */
  66.     }
  67.  
  68.     public boolean verificaPrimo(int numero) {
  69.         int divisores = 0;
  70.         for (int i = numero; i > 0; i--) {
  71.             if (numero % i == 0) {
  72.                 divisores++;
  73.             }
  74.         }
  75.         return (divisores == 2);
  76.         /*
  77.         System.out.println("Digite um número: ");
  78.         int numero = s.nextInt();
  79.         System.out.println(b.verificaPrimo(numero) ? "É primo!" : "Não é primo!");
  80.          */
  81.     }
  82.  
  83.     public int somaN(int numero) {
  84.         int soma = 0;
  85.         for (int i = 1; i <= numero; i++) {
  86.             soma += i;
  87.         }
  88.         return soma;
  89.         /*
  90.         System.out.println("Digite um número: ");
  91.         int numero = s.nextInt();
  92.         System.out.println("A soma de seus antecessores até 1 é: " + b.somaN(numero));
  93.          */
  94.     }
  95.  
  96.     public int calculaResto(int a, int b) {
  97.         return a % b;
  98.         /*
  99.         System.out.println("Digite um número: ");
  100.         int aa = s.nextInt();
  101.         int bb = s.nextInt();
  102.         System.out.println("O resto da divisão de "+aa+" e "+bb+" é: " + b.calculaResto(aa, bb));
  103.          */
  104.     }
  105.  
  106.     public void exibeSequencial(int numero){
  107.         int n1, n2, n3, n4, n5, n6;
  108.         n1 = numero%10;// - (numero/10*10);
  109.         n2 = (numero%100) / 10;// - (numero/100*10);
  110.         n3 = (numero%1000) / 100;// - (numero/1000*10);
  111.         n4 = (numero%10000) / 1000;// - (numero/10000*10);
  112.         n5 = (numero%100000) / 10000;// - (numero/100000*10);
  113.         n6 = (numero%1000000) / 100000;// - (numero/1000000*10);
  114.        
  115.         System.out.println(n6+" "+n5+" "+n4+" "+n3+" "+n2+" "+n1);
  116.         /*
  117.         System.out.println("Digite um número: ");
  118.         int numero = s.nextInt();
  119.         b.exibeSequencial(numero);
  120.         */
  121.     }
  122.     /*
  123.     public int exibeSequencia(int numero) {
  124.         if(numero / 1000000 != 0){
  125.             System.out.print(numero / 1000000+" ");
  126.             numero %= 1000000;
  127.         }else if(numero / 100000 != 0){
  128.             System.out.print(numero / 100000+" ");
  129.             numero %= 100000;
  130.         }else if(numero / 10000 != 0){
  131.             System.out.print(numero / 10000+" ");
  132.             numero %= 10000;
  133.         }else if(numero / 1000 != 0){
  134.             System.out.print(numero / 1000+" ");
  135.             numero %= 1000;
  136.         }else if(numero / 100 != 0){
  137.             System.out.print(numero / 100+" ");
  138.             numero %= 100;
  139.         }else if(numero / 10 != 0){
  140.             System.out.print(numero / 10+" ");
  141.             numero %= 10;
  142.         }else{
  143.             System.out.println(numero);
  144.             return 0;
  145.         }
  146.         return exibeSequencia(numero);
  147.  
  148.     }
  149.     */
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement