Bananaware

coelhinhos

Sep 30th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. public class Main
  2. {
  3.     public static void main(String coelhinhos[])
  4.     {
  5.         final int LIMIT = 150;
  6.         Exercicios e = new Exercicios(LIMIT);
  7.  
  8.         System.out.println("Pares entre 1 e " + LIMIT);
  9.         e.ex1();
  10.  
  11.         System.out.println("\n\nPrimos ateh " + LIMIT);
  12.         e.ex2();
  13.  
  14.         System.out.println("\n\nFibonacci ateh "  + LIMIT);
  15.         e.ex3();
  16.     }
  17. }
  18.  
  19. class Exercicios
  20. {
  21.     int limit = 100;
  22.  
  23.     public Exercicios(int limit)
  24.     {
  25.         this.limit = limit;
  26.     }
  27.  
  28.  
  29.    /* pares entre 1 e limit */
  30.     void ex1()
  31.     {
  32.         for(int i = 2; i <= limit; i+=2) // hue
  33.         {
  34.             System.out.print(i + " ");
  35.         }
  36.     }
  37.  
  38.     /* primos ateh limit */
  39.     void ex2()
  40.     {
  41.         for(int i = 2; i <= limit; i++)
  42.         {
  43.             boolean flag = false;
  44.             for(int j = 2; j < i && !flag; j++)
  45.             {
  46.                 if(i%j == 0)
  47.                 {
  48.                     flag = true;
  49.                 }
  50.             }
  51.             if(!flag)
  52.             {
  53.                 System.out.print(i + " ");
  54.             }
  55.         }
  56.     }
  57.  
  58.     /* fibonacci ateh limit */
  59.     void ex3()
  60.     {
  61.         int a = 1;
  62.         int b = 1;
  63.         int c = a+b;
  64.  
  65.         System.out.print(a + " " + b + " ");
  66.  
  67.         while(c <= limit)
  68.         {
  69.             System.out.print(c + " ");
  70.             a = b;
  71.             b = c;
  72.             c = a+b;
  73.         }
  74.  
  75.         System.out.println();
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment