Pages

Saturday, July 28, 2012

Salutation to the Dawn


Listen to the Salutation of the Dawn!
Look to this day!
For it is life, the very life of life,
In its brief course
Lie all the verities and realities of your existence:
The bliss of growth
The glory of action
The splendor of beauty,
For yesterday is but a dream
And tomorrow only a vision,
But today well lived makes every yesterday
a dream of happiness
And every tomorrow a vision of hope.
Look well, therefore, to this day!
Such is the salutation of the dawn

Salutation to the Dawn is a famous poem by Kalidasa. Based on the Sanskrit, c. 1200 B.C. This poem was mentioned in the great book "How to Stop Worrying and start living" by Dale Carnegie

Thursday, July 19, 2012

Java Program Exercises

Fibonacci

import java.util.Scanner;

public class Fibonacci {
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         System.out.print("Enter number: ");
         long f = sc.nextLong();
         System.out.printf("fib(%d) = %d", f, fib(f));
         System.exit(0);
     }

     public static long fib(long x) {
         if(x==0 || x==1)
             return x;
         else
             return fib(x-1) + fib(x-2);
     }
}

Factorial

import java.util.Scanner;
public class Factorial {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number: ");
        long f = sc.nextLong();
        System.out.printf("n(%d)! = %d", f, factorial(f));
        System.exit(0);
    }
    public static long factorial(long x) {
        if(x==1)
            return x;
        else
            return x * factorial(x-1);
    }
}

FizzBuzz

public class FizzBuzz {
    public static void main(String[] args) {
        for(int i=1;i<=100;i++) {
            System.out.printf("%d = %s\n",i, fizzBuzz(i));
        }
        System.exit(0);
    }
    public static String fizzBuzz(int x){
        if(divByThree(x) && divByFive(x))
            return "FizzBuzz";
        else if(divByFive(x))
            return "Buzz";
        else if(divByThree(x))
            return "Fizz";
        else
            return Integer.toString(x);
    }
    public static boolean divByThree(int x){
        return x%3==0;
    }
    public static boolean divByFive(int x){
        return x%5==0;
    }
}