Tuesday, May 21, 2013

Program to print Tribonacci Series using Recursive Technique


/**
 * class tribonacci_ser_recur prints tribonacci series using recursive technique.
 * @author: Nitendra Kumar
 * @version: May 21/2013
 */
import java.util.Scanner;
public class tribonacci_ser_recur
{
   public int trib(int n)
   {
       if(n==1)
         return 0;
         else if(n==2)
         return 1;
         else if(n==3)
         return 1;
       else if(n>3)
         return (trib(n-1)+trib(n-2)+trib(n-3));
         else 
         return -1;
    }
    
    public static void main()
    {
        int res;
        Scanner in=new Scanner(System.in);
        tribonacci_ser_recur ob=new tribonacci_ser_recur();
        System.out.println("Enter the limit");
        int limit=in.nextInt();
        for(int i=1;i<limit;i++)
        {  
        res=ob.trib(i);
          System.out.print(res+" ");
        }
    }
}

Output:


Enter the limit
10
0 1 1 2 4 7 13 24 44


/*---------------Program developed by: Nitendra Kumar---------------*/
//For more details visit http://javawithnitendra.blogspot.in

Program to check whether two numbers are Amicable Numbers

/**
 * class amicablenum checks whether a number is Amicable Number or not.
 * "Amicable numbers are a pair of numbers with the following property: the sum of all of the proper divisors of the first number (not including itself) 
 * exactly equals the second number while the sum of all of the proper divisors of the second number (not including itself) likewise equals the first number."
 * Ex: 220,284. proper divisors of 220:
 *1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110(sum=284)
 *proper divisors of 284:
 *1, 2, 4, 71, 142(sum=220)
 * @auther:Nitendra Kumar
 * @version: May 21/2013
 */
import java.util.Scanner;
public class amicablenum

 int checkamicable(int num1, int num2)
 {
     int sum1=0,sum2=0;
     System.out.println("Proper divisors of "+num1 +" are:");
     for(int i=1;i<=num1/2;i++)
     {
         if(num1%i==0)
         {
            System.out.print(" "+i);
            sum1+=i;
        }
    }

    System.out.println("\nProper divisors of "+num2 +" are:");
     for(int i=1;i<=num2/2;i++)
     {
         if(num2%i==0)
         {
            System.out.print(" "+i);
            sum2+=i;
        }
    }
    
    if(sum1==num2 && sum2==num1)
      return 1;
    else
      return 0;

}

 public static void main()
 {
     Scanner in=new Scanner(System.in);
     amicablenum ob=new amicablenum();
     int num1,num2;
     System.out.println("Enter 2 numbers to be checked for Amicable numbers");
     num1=in.nextInt();
     num2=in.nextInt();
     int flg=ob.checkamicable(num1,num2);
     if(flg==1)
        System.out.println("\nNumbers are Amicable numbers");
     else
        System.out.println("\nNumbers are not Amicable numbers");     
    }
}

Output:


Enter 2 numbers to be checked for Amicable numbers
220
284
Proper divisors of 220 are:
 1 2 4 5 10 11 20 22 44 55 110
Proper divisors of 284 are:
 1 2 4 71 142
Numbers are Amicable numbers



/*---------------Program developed by: Nitendra Kumar---------------*/
//For more details visit http://javawithnitendra.blogspot.in

Tuesday, May 14, 2013

Converting Time into Words


/**
 *This program converts the time into words.
 * @author : Nitendra Kumar 
 */

import java.util.Scanner;
class timetonum
{
public void display_time()
{
 Scanner in=new Scanner(System.in);
 System.out.println("Enter hours...(1-12)");
 int hh=in.nextInt();
 System.out.println("Enter minutes...(0-59)");
 int mm=in.nextInt();

 String[] time={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",
             "Eighteen","Nineteen","Twenty","Twenty One","Twenty Two","Twenty Three",
             "Twenty Four","Twenty Five","Twenty Six","Twenty Seven","Twenty Eight",
             "Twenty Nine"};
        
             
  if(hh>12 || mm>60)
    System.out.println("Invalid time");
  else if(mm==0)
    System.out.println(time[hh-1] + " o'clock");
    else if(mm==30)
    System.out.println("Half past " +time[hh-1]);
    else if(mm==15)
     System.out.println("Quarter past " +time[hh-1]);
    else if(mm==45)
      System.out.println("Quarter to " +time[hh]);
      
    else if(mm<30 && mm!=15)
      System.out.println(time[mm-1] +" minutes past " +time[hh-1]);
    else if(mm>30 && mm!=45)
      System.out.println(time[60-mm-1] + " minutes to " +time[hh]);
      
 }

   public static void main()
   {
       timetonum ob=new timetonum();
       ob.display_time();
    }
}

OUTPUT: 

Enter hours...(1-12)
5
Enter minutes...(0-59)
10
Ten minutes past Five


/*---------------Program developed by: Nitendra Kumar---------------*/
//For more details visit http://javawithnitendra.blogspot.in