Advertisement
Guest User

Fevre Lec 1 Java

a guest
Feb 9th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package org.gcc.dsa;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class LecMain_
  6. {
  7.  
  8. public static void main(String[] args)
  9. {
  10. // TODO Auto-generated method stub
  11. System.out.print("Factoriel 3 : ");
  12. System.out.println(fact(3));
  13.  
  14. //readlist(5, new Scanner(System.in));
  15.  
  16. System.out.print("505 : ");
  17. putDigits(505);
  18.  
  19. System.out.println("");
  20. System.out.print("Fibonacci 10 : ");
  21. System.out.println(Fibo(10));
  22.  
  23. long startTime = System.currentTimeMillis();
  24. System.out.print("Fibonacci 20 : ");
  25. Itefibo(20);
  26. long endTime = System.currentTimeMillis();
  27.  
  28. System.out.println(endTime - startTime);
  29. }
  30.  
  31. public static int fact(int n)
  32. {
  33. if (n==0)
  34. return 1;
  35. else
  36. return (n * fact(n-1));
  37. }
  38.  
  39. public static void readlist(int n, Scanner in)
  40. {
  41. if(n==0);
  42. else
  43. {
  44. int x = in.nextInt();
  45. System.out.println(x);
  46. readlist(n -1, in);
  47. }
  48. }
  49.  
  50. public static void putDigits(int n)
  51. {
  52. if(n == 0);
  53. else
  54. {
  55. int nb = n%10;
  56. putDigits(n/10);
  57. switch(nb)
  58. {
  59. case 0: System.out.print("Zero ");
  60. break;
  61. case 1: System.out.print("One ");
  62. break;
  63. case 2: System.out.print("Two ");
  64. break;
  65. case 3: System.out.print("Three ");
  66. break;
  67. case 4: System.out.print("Four ");
  68. break;
  69. case 5: System.out.print("Five ");
  70. break;
  71. case 6: System.out.print("Six ");
  72. break;
  73. case 7: System.out.print("Seven ");
  74. break;
  75. case 8: System.out.print("Eight ");
  76. break;
  77. case 9: System.out.print("Nine ");
  78. break;
  79. }
  80. }
  81. }
  82.  
  83. public static int Fibo(int n)
  84. {
  85. if(n < 0)
  86. return 0;
  87. if(n==0 || n==1)
  88. return 1;
  89. return (Fibo(n-1) + Fibo(n-2));
  90. }
  91.  
  92. public static int Itefibo(int n)
  93. {
  94. int x = 0;
  95. int a = 1;
  96. int b = 1;
  97. System.out.print("1 ");
  98. for (int i = 0; i < n; i++)
  99. {
  100. x = a + b;
  101. if(a < b)
  102. a = x;
  103. else
  104. b = x;
  105. System.out.print(x + " ");
  106. }
  107. System.out.println("");
  108. return x;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement