Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.gcc.dsa;
- import java.util.Scanner;
- public class LecMain_
- {
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- System.out.print("Factoriel 3 : ");
- System.out.println(fact(3));
- //readlist(5, new Scanner(System.in));
- System.out.print("505 : ");
- putDigits(505);
- System.out.println("");
- System.out.print("Fibonacci 10 : ");
- System.out.println(Fibo(10));
- long startTime = System.currentTimeMillis();
- System.out.print("Fibonacci 20 : ");
- Itefibo(20);
- long endTime = System.currentTimeMillis();
- System.out.println(endTime - startTime);
- }
- public static int fact(int n)
- {
- if (n==0)
- return 1;
- else
- return (n * fact(n-1));
- }
- public static void readlist(int n, Scanner in)
- {
- if(n==0);
- else
- {
- int x = in.nextInt();
- System.out.println(x);
- readlist(n -1, in);
- }
- }
- public static void putDigits(int n)
- {
- if(n == 0);
- else
- {
- int nb = n%10;
- putDigits(n/10);
- switch(nb)
- {
- case 0: System.out.print("Zero ");
- break;
- case 1: System.out.print("One ");
- break;
- case 2: System.out.print("Two ");
- break;
- case 3: System.out.print("Three ");
- break;
- case 4: System.out.print("Four ");
- break;
- case 5: System.out.print("Five ");
- break;
- case 6: System.out.print("Six ");
- break;
- case 7: System.out.print("Seven ");
- break;
- case 8: System.out.print("Eight ");
- break;
- case 9: System.out.print("Nine ");
- break;
- }
- }
- }
- public static int Fibo(int n)
- {
- if(n < 0)
- return 0;
- if(n==0 || n==1)
- return 1;
- return (Fibo(n-1) + Fibo(n-2));
- }
- public static int Itefibo(int n)
- {
- int x = 0;
- int a = 1;
- int b = 1;
- System.out.print("1 ");
- for (int i = 0; i < n; i++)
- {
- x = a + b;
- if(a < b)
- a = x;
- else
- b = x;
- System.out.print(x + " ");
- }
- System.out.println("");
- return x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement