Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- /**
- *
- *
- */
- public class Tribonacci {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- try {
- System.out.print("Please Enter a number :");
- Integer n = Integer.parseInt(bf.readLine());
- if(n < 0){
- throw new NegativeNumberException("Negative Numbers are not allowed!!");
- }
- else if(n > 0 && n < 4){
- throw new InvalidInputException("You must not enter a valid input above 4");
- }
- else{
- int a=0,b=1,c=1,sum=0;
- System.out.print("The Tribonacci Serices is :\n 0\t 1\t 1\t");
- while(n > 3){
- sum = a+b+c;
- System.out.print(sum +"\t");
- n--;
- a = b;
- b = c;
- c = sum;
- }
- }
- }catch (NumberFormatException e) {
- // TODO Auto-generated catch block
- System.out.print("Please enter Numbers only !!");
- } catch (NegativeNumberException e){
- System.out.print(e.getMessage());
- }catch (InvalidInputException e){
- System.out.print(e.getMessage());
- }
- catch (IOException e) {
- // TODO Auto-generated catch block
- System.out.print("Unexpceted error !!");
- }
- }
- }
- class InvalidInputException extends Exception{
- private static final long serialVersionUID = 1L;
- public InvalidInputException(String msg){
- super(msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement