Thursday, December 26, 2013

Program - Factorial / Perfect No / Prime No.



Factorial of a given Number.

Factorial of a number is the product of all number from 1 to 'n'.

Ex. Factorial of 5!=5*4*3*2*1 = 120.


import java.io.*;
    class Factorial
        {
            public static void main(String args[])throws IOException
                {
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n;
                    System.out.print("\tEnter a No : ");
                    n=Integer.parseInt(Br.readLine());
                    Factorial Ft=new Factorial();
                    long F=Ft.Fact(n);
                    System.out.println("\n\tFactorial of '"+n+"' = "+F);
                   
                }
            long Fact(int n)
                {
                    long F=1;
                    for(;n>1;n--)
                        {
                            F*=n;
                        }
                    return F;   
                }
        }



Output

          Enter a No : 5
          Factorial of '5' = 120


Perfect No is a number where sum of all factor of a number equals to the number itself.

Ex. 6 = (1+2+3+6)/2  = 6  or  1+2+3 = 6

import java.io.*;
    class Perfect
        {
            public static void main(String args[])throws IOException
                {
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n;
                    System.out.print("\tEnter a No : ");
                    n=Integer.parseInt(Br.readLine());
                    Perfect P = new Perfect();
                    if(P.Sum_Factor(n)==n)
                        {
                            System.out.println("\n\t '"+n+"' is a Perfect No");
                        } 
                    else
                        {
                            System.out.println("\n\t '"+n+"' is not a Perfect No");
                        }                          
                }
            int Sum_Factor(int n)
                {
                    int s=0;
                    for(int i=1;i<=n/2;i++)
                        {
                            if(n%i==0)
                                {
                                    s+=i;
                                }
                        }
                    return s;   
                }
        }



Output
          Enter a No : 6
           '6' is a Perfect No

          Enter a No : 12
           '12' is not a Perfect No


Prime No is the numbers that divided by 1 and itself or prime numbers only can have two factors.

Ex. 5 is only divisible by 1 and 5.


import java.io.*;
    class Prime
        {
            public static void main(String args[])throws IOException
                {
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n;
                    System.out.print("\tEnter a No : ");
                    n=Integer.parseInt(Br.readLine());
                    Prime Pr=new Prime();
                    if(Pr.isPrime(n)==0)
                        {
                            System.out.println("\n\t"+n+" is not Prime");
                        }
                    else
                        {
                            System.out.println("\n\t"+n+" is Prime");
                        }                       
                }
            int isPrime(int n)
                {
                    int i;;
                    for(i=2;i<=n/2;i++)
                        {
                            if(n%i==0)
                                {
                                    return 0;
                                }
                        }
                    return 1;   
                }
        }



Output

          Enter a No : 23
          23 is Prime

          Enter a No : 54
          54 is not Prime




Thursday, December 19, 2013

Math Function


Math Functions - The class Math from package java.lang contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.


Some Useful Mathematic Function

Math.abs(double) - Returns the absolute value of a double value.
Math.sqrt(double) - Returns the square root of a double value.
Math.pow(double, double) - Returns of value of the first argument raised to the power of the second argument.
Math.min(double, double) – Returns the smaller of two double values.
Math.max(double, double) – Returns the larger of two double values.
Math.sin(double) / Math.cos(double) / Math.tan(double) - Returns the trigonometric sine, cosine and tangent of an angle.
Math.exp(double) - Returns the exponential number e (i.e., 2.718...) raised to the power of a double value.
Math.ceil(double) - Returns the smallest  (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer. Math. ceil(12.5) = 13, Math.ceil(-12.5) =  -12
Math.floor(double) - Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer. Math.floor(12.5) = 12, Math.floor(-12.5) = -13.
Math.random(double) - Returns a random number between 0.0 and 1.0.
Math.round(double) - Returns the closest long to the argument.
Math.rint(double) - Returns the closest double value that is equal to a mathematical integer.. e.g. double d=56.678; will give 57.0 and double d=56.478 will give 56.0.



Example :

import java.io.*;
    class Math_Functions
        {
            public static void main(String args[])throws IOException
                {
                    Math_Functions MF=new Math_Functions();
                    MF.Sqrt();
                    MF.Power();
                    MF.Trigonometric_Fun();
                    MF.Max_Min();
                    MF.Ceil_Floor();
                    MF.Round();
                    MF.Rint();
                    MF.ABS();
                    MF.Random();
                 }
            void Sqrt()
                {
                    double num=12,sq=0;
                    sq=Math.sqrt(num);
                    System.out.println("\n\tSquare Root '"+num+"' = "+sq);
                    
                }
            void Power()
                {
                    double x=5,y=3.5,pw;
                    pw=Math.pow(x,y);
                    System.out.println("\n\t"+x+"^"+y+" = "+pw);
                   
                }                       
            void Trigonometric_Fun()
                {
                    System.out.println("\n\tTrignometic Functions : ");
                    double Ang=90,Rad=0.0,Result=0.0;
                    Rad=Math.toRadians(Ang);
                    System.out.println("\n\t Radian of "+Ang+" = "+Rad);
                    Result=Math.sin(Rad);
                    System.out.println("\n\t Sine of "+Ang+" = "+Result);
                    Result=Math.cos(Rad);
                    System.out.println("\n\t Cosine of "+Ang+" = "+Result);
                    Result=Math.tan(Rad);
                    System.out.println("\n\t Tan of "+Ang+" = "+Result);
                } 
            void Max_Min()
                {
                    int a=15,b=35;
                    System.out.println("\n\tMaximum of "+a+" and " + b+" = "+Math.max(a,b));
                    System.out.println("\n\tMinimum of "+a+" and " + b+" = "+Math.min(a,b));                   
                }  
            void Ceil_Floor()
                {
                    double a=15.5,b=-35.5;
                    System.out.println("\n\tFloor "+a+" is = "+Math.floor(a));
                    System.out.println("\n\tCeil "+a+" is = "+Math.ceil(a));
                    System.out.println("\n\tFloor "+b+" is = "+Math.floor(b));
                    System.out.println("\n\tCeil "+b+" is = "+Math.ceil(a));
                } 
            void Round()
                {
                    double x=145.5,y=0.0;
                    y=Math.round(x);
                    System.out.println("\n\tRounded value of "+x+" to "+y);
                   
                }       
            void Rint()
                {
                    double x=15.323,y=0.0;
                    y=Math.rint(x);
                    System.out.println("\n\tRint of "+x+" = "+y);
                    x=15.5;
                    y=Math.rint(x);
                    System.out.println("\n\tRint of "+x+" = "+y);
                }  
            void ABS()
                {
                    int a=-15,b=35;
                    System.out.println("\n\tAbsoulute Value of "+a+" = "+Math.abs(a));
                    System.out.println("\n\tAbsoulute Value of "+b+" = "+Math.abs(b));               
                }
            void Random()
                {
                    int a=2;
                    System.out.println("\n\tRandom Nos. in a range of 100\n\t");
                    System.out.print("\t");
                    for(int i=1; i<=10;i++)
                        {
                            System.out.print((int)(Math.random()*100)+" ");               
                        }
                }               
               
        }


Output:

          Square Root           '12.0' = 3.4641016151377544
                             5.0^3.5 = 279.5084971874737
      Trignometic Functions :
                    Radian of 90.0 = 1.5707963267948966
                    Sine of 90.0 = 1.0
                    Cosine of 90.0 = 6.123233995736766E-17
                    Tan of 90.0 = 1.633123935319537E16
          Maximum of 15 and 35 = 35
          Minimum of 15 and 35 = 15
          Floor 15.5 is = 15.0
          Ceil 15.5 is = 16.0
          Floor -35.5 is = -36.0
          Ceil -35.5 is = 16.0
          Rounded value of 145.5 to 146.0
          Rint of 15.323 = 15.0
          Rint of 15.5 = 16.0
          Absoulute Value of -15 = 15
          Absoulute Value of 35 = 35
          Random Nos. in a range of 100
                   10 44 46 64 79 20 49 60 45 51


Thursday, December 12, 2013

Recursion



Recursion is a programming technique used in place of iteration. Methods, procedures or function used recursion calls itself until given condition is satisfied. Often required multiple loops and it make the program too complex. The loops can be replace into small functions with recursive technique and it simplified the program and easy to understand. However, recursive function had some disadvantage too. The recursive needs more space and time as the variables used in function call
¨       Linear Recursion
¨       Tail Recursion
¨       Binary Recursion
¨       Mutual Recursion
¨       Nested Recursion

1.    Linear Recursion is a function that calls itself in a simple manner. The function terminates and return to the caller function when given condition satisfied. The process is known as ‘Winding’ and 'Un-Winding'. The condition given for termination once reach the required limits and to return to the caller. The termination condition also called as Base condition.
Example:
     int fact(int n)
         {
              if(n<=1)
                 return 1;
              else
                    return ( n* fact(n-1));
         }
 
2.    Tail Recursion - When recursive call occurs at the end of a method it is called a tail recursion. The function perform all the statements before hopping to the next call.

Example:

     int gcd(int x, int y)
        {
             if(y<=0)
                  return x ;
             else
                  return(y,x%y) ;
        }
   
3.    In binary recursion, a function calls itself twice.
  Example:
         void fibo(int n)
         
               if( n <= 2 )
                            return 1;
               else
                          return fib(n-1)+fib(n-2);  
         }


4.    Mutual Recursion when the functions called each other. In following example the fun

Example:

     boolean Even(int n)
        {
             if(n%2==0)
                  {
                       return true;
                  }
             else
                       return Odd(n);
        }

     boolean Odd(int n)
        {
             if(n%2!=0)
                  {
                       return false;
                  }
             else
                       return Even(n);
        }

5.    Nested Recursion is an exception as all other recursive function can be written using loops except this one.  In nested recursive, each recursive function calls another recursive function and this cannot be replace by simple iteration.

Example:
 
    int Num(int n)
     {
         if(n>= 10)
             return  n - 1;
         else
             return Num(Num(n + 3));
     }