Advertisement
rutera

Tribonacci

Oct 19th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. /**
  6.  *
  7.  *
  8.  */
  9. public class Tribonacci {
  10.  
  11.     /**
  12.      * @param args
  13.      */
  14.     public static void main(String[] args) {
  15.         // TODO Auto-generated method stub
  16.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  17.         try {
  18.             System.out.print("Please Enter a number :");
  19.             Integer n = Integer.parseInt(bf.readLine());
  20.            
  21.             if(n < 0){
  22.                 throw new NegativeNumberException("Negative Numbers are not allowed!!");
  23.             }
  24.             else if(n > 0 && n < 4){
  25.                 throw new InvalidInputException("You must not enter a valid input above 4");
  26.             }
  27.             else{
  28.                 int a=0,b=1,c=1,sum=0;
  29.                 System.out.print("The Tribonacci Serices is :\n 0\t 1\t 1\t");
  30.                 while(n > 3){
  31.                     sum = a+b+c;
  32.                     System.out.print(sum +"\t");
  33.                     n--;
  34.                     a = b;
  35.                     b = c;
  36.                     c = sum;
  37.                 }  
  38.             }
  39.         }catch (NumberFormatException e) {
  40.             // TODO Auto-generated catch block
  41.             System.out.print("Please enter Numbers only !!");
  42.         } catch (NegativeNumberException e){
  43.             System.out.print(e.getMessage());
  44.         }catch (InvalidInputException e){
  45.             System.out.print(e.getMessage());
  46.         }
  47.         catch (IOException e) {
  48.             // TODO Auto-generated catch block
  49.             System.out.print("Unexpceted error !!");
  50.         }
  51.        
  52.  
  53.     }
  54.  
  55. }
  56.  
  57. class InvalidInputException extends Exception{
  58.     private static final long serialVersionUID = 1L;
  59.  
  60.     public InvalidInputException(String msg){
  61.         super(msg);
  62.     }
  63.    
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement