Tuesday, December 29, 2015

Java program to find greatest number in array


Java program to find greatest number in array:



package arraydemo;


public class ArrayDemo {

    
    public static void main(String[] args) {
        
        
                
                int numbers[] = new int[]{11,12,13,44,55,66,77,88,99,23};
               
                
                int small = numbers[0];
                int large = numbers[0];
               
                for(int i=1; i< numbers.length; i++)
                {
                        if(numbers[i] > large)
                                large = numbers[i];
                        else if (numbers[i] < small)
                                small = numbers[i];
                       
                }
               
                System.out.println("Largest Number is : " + large);
                System.out.println("Smallest Number is : " + small);
        }
}
OUTPUT:
Largest Number is : 99

Smallest Number is : 11

Monday, December 28, 2015

Simple Java Program to calculate Restaurant Bill

 Simple Java Program to calculate Restaurant Bill:




package restaurants;

import java.util.Scanner;


public class Restaurants {

    
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        boolean quit= false;
        int sum=0;
        int wine=200,cold=20,bear=400,juice=100;
        String order="";
        
        do{
            System.out.println("ITEM"+"\t\tPrice");
            System.out.println("1.Wine"+"\t\t"+"200");
            System.out.println("2.ColdDrink"+"\t"+"20");
            System.out.println("3.Bear"+"\t\t"+"400");
            System.out.println("4.Juice"+"\t\t"+"100");
            System.out.println("5.Quit");
            
            int choice=input.nextInt();
            
            switch(choice){
                case 1:System.out.println("Wine"+"\n");
                        sum=sum+wine;
                        order=order.concat("wine"+"\n");
                        
                    break;
                case 2:
                    System.out.println("ColdDrink");
                    sum=sum+cold;
                    order=order.concat("ColdDrink"+"\n");
                    
                       break;
                case 3:
                    System.out.println("Bear");
                    sum=sum+bear;
                    order=order.concat("Bear"+"\n");
                      break;
                case 4:
                    System.out.println("Juice");
                    sum=sum+juice;
                    order=order.concat("Juice"+"\n");
                    break;
                case 5:
                     quit=true;
                     
                    break;
                default:
                    System.out.println("Wrong input");
            }
        
        }while(!quit);
       
        System.out.println("Your Orders are:\n"+order);
        System.out.println("Your total bill="+sum);
        
         System.out.println("Thank you");
    
        
        
        
    
    }
    
}


OUTPUT:
ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
1
Wine

ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
2
ColdDrink
ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
3
Bear
ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
4
Juice
ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
5
Your Orders are:
wine
ColdDrink
Bear
Juice

Your total bill=720
Thank you

Sunday, December 27, 2015

Java KingKong Program

Java KingKong Program:



package javaapplication16;


public class KingKongDemo {
    public static void main(String[] args) {
        for(int i=1;i<=50;i++){
        
            if(i%3==0){
                System.out.println(i+"King");
            }
            else if(i%5==0){
                System.out.println(i+"kong");
            }
            else if(i%3==0 && i%5==0){
                System.out.println("KingKong");
            }else{
                System.out.println(i);
            }
        }
    }
}

OUTPUT:

1
2
3King
4
5kong
6King
7
8
9King
10kong
11
12King
13
14
15King
16
17
18King
19
20kong
21King
22
23
24King
25kong
26
27King
28
29
30King
31
32
33King
34
35kong
36King
37
38
39King
40kong
41
42King
43
44
45King
46
47
48King
49
50kong



Relational Operator

Relational Operator:


Operators                             Result

==                                          Equql to

!=                                           Not Equal to

>                                            Greater Than

<                                             Lesser than

>=                                           Greater than or equal to

<=                                           Lesser than or equal to  




package javaapplication16;


public class RelationalOperatorDemo {
    public static void main(String[] args) {
        int a=1;
        int b=2;
        int c=1;
        if(a==c){
            System.out.println(" a is Equal to c"+a);
        }else{
            System.out.println("a is not equal to c");
        }
        
        if(a!=b){
            System.out.println("a is Not Equal to b");
        }
        
        if(a>b){
            System.out.println("a is greater than b");
        }
           if(a<b){
               System.out.println("a is less than b");
           }  
           
           if(a>=b){
               System.out.println("a is greater or eual to b");
           }else{
            System.out.println("a is  not greater or eual to b");
           }
           
           if(a<=b){
               System.out.println("a is greater or equal to b");
           }else{
           System.out.println("a is not greater or equal to b");
           }
    }
}

Output:
 a is Equal to c1
a is Not Equal to b
a is less than b
a is  not greater or eual to b
a is greater or equal to b

Arithmetic Operators

Arithmetic Operators:




package javaapplication16;


public class ArithemiticOperatorDemo {
    public static void main(String[] args) {
        int a=10;
        int b=5;
        
        int c=a+b;
        int d=a/b;
        int e=a*b;
        int f=a%b;
        
        System.out.println("Arithmetic Operator Operation");
        System.out.println("Addition = "+a);
        System.out.println("Division = "+b);
        System.out.println("Multiplification = "+e);
        System.out.println("Modulus = "+f);
    }
}

OUTPUT:
Arithmetic Operator Operation
Addition = 10
Division = 5
Multiplification = 50
Modulus = 0

Increment And Decrement Operator

Increment and Decrement Operator:


package javaapplication16;


public class IncrementAndDecrementOPeratorDemo {
    public static void main(String[] args) {
        int a=5;
        int b=3;
        int c;
        int d;
        
        c= ++b;
        d=++a;
        c++;
        
        System.out.println("a = "+a);
        System.out.println("b = "+b);
        System.out.println("c = "+c);
        System.out.println("d= "+d);
    }
}

OUTPUT:
a = 6
b = 4
c = 5
d= 6

Saturday, December 26, 2015

Java Program to add two Matrix

Java Program to add two Matrix:


package javaapplication22;

import java.util.Scanner;


public class AddTwoMatrix {
    public static void main(String[] args) {
       Scanner s = new Scanner(System.in);

       System.out.print("Enter number of rows: ");
       int rows = s.nextInt();

       System.out.print("Enter number of columns: ");
       int cols = s.nextInt();

       int[][] a = new int[rows][cols];
       int[][] b = new int[rows][cols];

       System.out.println("Enter the first matrix");
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               a[i][j] = s.nextInt();
           }
       }
       System.out.println("Enter the second matrix");
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               b[i][j] = s.nextInt();
           }
       }
       int[][] c = new int[rows][cols];
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               c[i][j] = a[i][j] + b[i][j];
           }
       }
       System.out.println("The sum of the two matrices =");
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               System.out.print(c[i][j] + " ");
           }
           System.out.println();
       }
   }
}

Output:
Enter number of rows: 2
Enter number of columns: 2
Enter the first matrix
1 2
3 4
Enter the second matrix
1 4
3 5
The sum of the two matrices =
2 6 
6 9 

Java BizzBuzz Program

Java BizzBuzz Program:


package javaapplication22;


public class BizzBuzzDemo {
   
    public static void main(String[] args) {
        for(int i=1;i<=50;i++){
            if(i%3==0){
                System.out.println("Bizz"+i);
            }else if(i%5==0){
                System.out.println("Buzz"+i);
            }else if(i%3==0 && i%5==0){
                System.out.println("BizzBuzz"+i);
            }else{
                System.out.println(i);
            }
       
        }
    }
}
OUTPUT:


1
2
Bizz3
4
Buzz5
Bizz6
7
8
Bizz9
Buzz10
11
Bizz12
13
14
Bizz15
16
17
Bizz18
19
Buzz20
Bizz21
22
23
Bizz24
Buzz25
26
Bizz27
28
29
Bizz30
31
32
Bizz33
34
Buzz35
Bizz36
37
38
Bizz39
Buzz40
41
Bizz42
43
44
Bizz45
46
47
Bizz48
49
Buzz50


IndexOf() and last indexOf()

Searching Strings:

1)IndexOf() = searches for the first occur of a character 
                           int indexOf(int ch);

2)last indexOf()=searches for the last occur of a character 
                              int lastIndexOf(int ch);






package stringdemo;


public class IndexOfAndlastIndexOfDemo {
    
    public static void main(String[] args) {
        String s="I am a bad guy";
        
        System.out.println(s);
        System.out.println("indexOf(a)"+s.indexOf('a'));
        System.out.println("lastindexOf(g)"+s.lastIndexOf('a'));
    }
    
}
Output:
I am a bad guy
indexOf(a)2
lastindexOf(g)8

Friday, December 25, 2015

length() and capacity() method

StringBuffer:



length() and capacity()

The length of the StringBuffer can be found by the length() method.while the total capacity can be found through the capacity() method.They have the following forms:

                      int length();
                      int capacity();




package stringdemo;


public class LengthAndCapacity {
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer("Dance");
        
        System.out.println("Length="+s.length());
        System.out.println("Capacity"+s.capacity());
    }
}
OUTPUT:
Length=5
Capacity21

Here Length is 5 and capacity is 21 because room for 16 additional characters is automatically added,so result is 21.

delete() and deleteCharAt() method

delete() and deleteCharAt() method:

Java  provides us to StringBuffer the ability to delete character using the method delete() and deleteCharAt(). These method are shown here:

         StringBuffer delete(int startIndex,int endIndex)
         StringBuffer deleteCharAt(int loc)

Here is a small program to demonstrate these method:


package stringdemo;


public class DeleteDemo {
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer("I love you");
        
        s.delete(2, 5);
        System.out.println("After delete:"+s);
        
        s.deleteCharAt(0);
        System.out.println("After deleteCharAt:"+s);
    }

}

OUTPUT:
After delete:I e you
After deleteCharAt: e you

Java program to show number pyramid

Number Pyramid:


package diamond;


public class PyramidDemo {
    public static void main(String[] args) {
        for(int i=0;i<5;i++) {
         for(int j=0;j<5-i;j++) {
             System.out.print(" ");
         }
        for(int k=0;k<=i;k++) {
            System.out.print("* ");
        }
        System.out.println();
    }

}
}


OUTPUT:
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 

Fibonacci Using Recusion method

Fibonacci Using Recusion method:

package diamond;

import static diamond.Fibonacci.fibonacciRecusion;


public class FibonacciDemo {
    public static void main(String[] args) {
        System.out.println("\nFibonacci by recusion");
        int febCount=15;
    for(int i=1; i<=febCount; i++){
      System.out.print(fibonacciRecusion(i) +" ");
    }
       
   
    }
}
The output of this program is as:
Fibonacci by recusion
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

Thursday, December 24, 2015

replace() method

Modifying a String:


replace();

    The replace method is used to replace the one character of string.It has the general form:
      
String replace(char original,char replace);


Here is a small program to show how replace() method works:


package stringdemo;


public class ReplaceDemo {
    public static void main(String[] args) {
        String s="HelloWorld";
       
        System.out.println(s.replace("H", "W"));
    }
}
Output:

WelloWorld


concat() and trim() method

concat()

You can concat two String using concat() as shown below:


package stringdemo;


public class Concat {
    public static void main(String[] args) {
        String s1="ram";
        String s2="john";
        
        System.out.println(s1.concat(s2));
    }

}
The output of the above program is:
ramjohn



trim()

trim() method of String in java is used to remove whitespace.It has the general form:
String trim();
Here is a small program to demonstrate trim()method:

package stringdemo;


public class Trim {
    public static void main(String[] args) {
        String s="  Hello World";
        
        System.out.println(s.trim());
    }
}
The output of the above program is:
Hello World

equals() Versus ==

equals() Versus ==

We should be clear that equals() method and == operator perform two different operations.The equals() method compares the character inside a string whereas == operator compares two object reference to see whether they refer to the same instances.Here is a small program to demonstrate the difference between equals()method vs == operator:


package stringdemo;


public class Equal {
    public static void main(String[] args) {
        String s1="john";
        String s2=new String(s1);
       
        System.out.println(s1.equals(s2));
        System.out.println(s1==s2);
    }
}
The above program gives the following output:
true
false

equals() and equalsIgnoreCase()

String Comparision:

The String class includes many methods that compares Strings or Substrings within strings.

equals() and equalsIgnoreCase()


package stringdemo;


public class StringComparision {
    public static void main(String[] args) {
        String str="john";
        String str1="john";
        String str2="JOHN";
        String str3=new String("john");
        
        
        if(str==str1){
            System.out.println("Equal");
        }else{
        System.out.println("Not Equal");
        }
        
        if(str.equals(str1)){
            System.out.println("str is equal to str1");
        }else{
        System.out.println("str is not equal to str1");
        }
        
        if(str==str2){
        System.out.println("str is equal to str2");
        }else{
        System.out.println("str is not equal to str2");
        }
        
        if(str.equals(str2)){
            System.out.println("equals");
        }else{
        System.out.println(" not equals");
        }
        if(str.equalsIgnoreCase(str2)){
        System.out.println("equalsss");
        }else{
        System.out.println(" not equalsss");
        }
        
        if(str1==str3){
            System.out.println("str1 is equal to str3");
        }else{
        
        System.out.println("str1 is not  equal to str3");
        }
        
        if(str.equals(str3)){
            System.out.println("bang");
        }else{
            System.out.println("not bang");
        }
        }
    }

   
    The output of the above program is as below:

Equal
str is equal to str1
str is not equal to str2
 not equals
equalsss
str1 is not  equal to str3
bang





Character Extraction

Character Extraction:


2)getChars()


If you want to extract more than one character at a time,you can use the getChars() method.
It has general form:
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart);


package stringdemo;


public class NewClass {
    public static void main(String[] args) {
       
      String s="This is the demo of getChar()method";
      int start=5;
      int end=8;
       char ch[]=new char[end-start];
     
       s.getChars(start, end, ch, 0);
        System.out.println(ch);
    }
}
The output is:
is 

Character Extraction

Character Extraction:


1) charAt()


charAt() method is used to extract single character from String.Here is a small program to demonstrate chatAt() method.  


package stringdemo;


public class NewClass {
    public static void main(String[] args) {
       
      String  ch="java";
        System.out.println(ch.charAt(1));
    }
}

The output of this program is as:
a

String Concatenation with other data type

String Concatenation with other data type:

package stringdemo;


public class StringS {
    public static void main(String[] args) {
        String s="nine:"+4+4;
        System.out.println(s);
    }
}

Here output will be like 
nine:44.
   rather than
nine:8

To get 2nd type of output we must change the 
String s="nine:"+(4+4);

This will generate the output as:
nine:8

String Concatenation:

String Con:catenation
Here is a small java program to demonstrate String Concatenation:

package stringdemo;


public class StringConcat {
    public static void main(String[] args) {
        String age="19";
        String s= "I am"+ age +"years old";
        System.out.println(s);
       
       
        //Using concatenation to prevent long lines.
       
        String str="I want to say u that"+"I love you";
        System.out.println(str);
    }
}
The output of this Program is shown below:


I am19years old
I want to say u thatI love you

String Length in java

String Length in java:


The length of the String is the actual number of character that contains in it.To get this value,call the length() method as shown below:
int length();

Here is a short program to demonstrate String length:

package stringdemo;


public class StringLengthDemo {
    public static void main(String[] args) {
        char ch[]={'a','e','i','o','u'};
        String s= new String(ch);
        System.out.println(s.length());
    }
}
 The output of this program is as below:

5

The String Constructor:

The String Constructor:
//Construct one String from other

class MakeString{
public Static void main(String[] args){

Char c[]={'d','o','g'};
String s1=new String(c);
String s2= new String(s1);

System.out.println(s1);
System.out.println(s2);
}

}
The output of this program is as follow:
dog
dog

Wednesday, December 23, 2015

Passing object as parameter in java:

Passing object as parameter in java:

package using_object_as_parameter;

public class Make {
    int a,b;
   
    Make(int i,int j){
    a=i;
    b=j;
    }
   
    boolean equals(Make o){
    if(o.a==a &&o.b==b)return true;
    else return false;
    }
}



public class Passob {
    public static void main(String[] args) {
        Make obj = new Make(100,22);
        Make obj1 = new Make(1,2);
        
        System.out.println("obj==obj1:"+obj.equals(obj1));
    }
}
The output of this code is:
run:
obj==obj1:false
BUILD SUCCESSFUL (total time: 3 seconds)

Access Protection in java:

Access Protection in java:



private     No modifier protected public

 Same class yes yes yes yes

 Same package      no yes yes yes
 subclass

 Same package no yes yes yes
 non-subclass


  Different               no no yes                  yes
  package
  subclass


  Different no no no yes
  package
  non-subclass

Method with Parameter in java

Method with Parameter in java
package javaapplication27;


public class MethodWithParameter {
    double width;
    double height;
    double depth;
   
   
     public double volume(){
return width * height * depth;

                }
    void setDim(double w,double h,double d){
    width=w;
    height=h;
    depth=d;
    }
   
    public static void main(String[] args) {
        MethodWithParameter obj= new MethodWithParameter();
       
        double vol;
       
        obj.setDim(5, 10, 15);
       vol=obj.volume();
       
        System.out.println("Volume is "+vol);
    }
}

 Output:
run:
Volume is 750.0

Stack in java:

Stack in java:

package javaapplication27;

import java.util.Stack;


public class TestStack {
   
    public static void main(String[] args) {
        Stack obj= new Stack();
        Stack obj1= new Stack();
       
        //push some number into the stack
       
        for(int i=0;i<10;i++)
            obj.push(i);
        for(int i=0;i<20;i++)
            obj1.push(i);
       
       
        //pop those numbers off the stack
       
        System.out.println("Stack in obj");
        for(int i=0;i<10;i++)
            System.out.println(obj.pop());
       
       
        System.out.println("Stack in obj1");
        for(int i=0;i<10;i++)
            System.out.println(obj1.pop());
           
    }
   
}
OUTPUT:

Stack in obj
9
8
7
6
5
4
3
2
1
0
Stack in obj1
19
18
17
16
15
14
13
12
11
10


Recursion Function in java:

Recursion Function in java:

public class Factorial {
    //this is recursive function
    int fact(int n){
    int result;
 
    if(n==1)return 1;
    result=fact(n-1)*n;
    return result;
    }
}




public class RecursionDemo {
 
    public static void main(String[] args) {
        Factorial f =new Factorial();
     
        System.out.println("Factorial of 3 is"+f.fact(3));
        System.out.println("Factorial of 4 is"+f.fact(4));
        System.out.println("Factorial of 5 is"+f.fact(5));
    }
 
}
Output:
Factorial of 3 is6
Factorial of 4 is24
Factorial of 5 is120

Overloading Constructor:

Overloading Constructor:

package overloadingconstrucror;

public class Box {
    double width;
    double height;
    double depth;
   
    Box(double w,double h,double d){
    width=w;
    height=h;
    depth=d;
    }
   
    Box(){
    width=-2;
    height=-2;
    depth=-2;
    }
   
    //construcror used when cube is created
    Box(double leng){
    width=height=depth=leng;
    }
    double volume(){
    return width*height*depth;
    }
}



public class OverloadConstructor {
    public static void main(String[] args) {
        Box obj = new Box(5,10,15);
        Box obj1= new Box();
        Box obj2 = new Box(6);
        
        double vol;
        
        vol=obj.volume();
        System.out.println("volume "+vol);
        
        vol=obj2.volume();
        System.out.println("Volume "+vol);
    }
}

The above code give the output as:
run:
volume 750.0
Volume 216.0

Tuesday, December 22, 2015

Java break statement:

Java break statement:

package javaapplication27;


public class BreakDemo {
    public static void main(String[] args) {
        for(int i=0;i<=100;i++){
        if(i==10)break;         //break loop if i is 10;
            System.out.println("i"+i);
        }
        System.out.println("loop complete.");
    }
}


The output  of this program is:
run:
i0
i1
i2
i3
i4
i5
i6
i7
i8
i9
loop complete.


Nested Loop:

Nested Loop:


package javaapplication27;


public class NestedLoop {
    public static void main(String[] args) {
        int i,j;
        for(i=1;i<=10;i++){
        for(j=1;j<=10;j++){
            System.out.println(".");
            System.out.println();
        }
       
        }
    }
}

Do-While loop:

Do-While loop:
package javaapplication27;

public class DowhileLoop {
    public static void main(String[] args) {
        int n=10;
        do{
            System.out.println("Go"+n);
            n--;
           
        }while(n>0);
    }
}

The output Is :
run:
Go10
Go9
Go8
Go7
Go6
Go5
Go4
Go3
Go2
Go1


java while loop

java while loop
package javaapplication27;


public class WhileloopDemo {
    public static void main(String[] args) {
        int n=10;
       
        while(n>0){
            System.out.println("go"+n);
            n--;
        }
    }
}

The output is :
run:
go10
go9
go8
go7
go6
go5
go4
go3
go2
go1


Java twodimensional array:

Java twodimensional array:

package javaapplication27;


public class TwoDArrayDemo {
 
    public static void main(String[] args) {
        int array[][]=new int[4][];    //declaring two dimensional array
     
       array[0]=new int[1];
       array[1]=new int[2];
       array[2]=new int[3];
       array[3]=new int[4];
     
       int i,j,k=0;
     
       for(i=0;i<4;i++)
           for(j=0;j<i+1;j++){
           array[i][j]=k;
           k++;
           }
       for(i=0;i<4;i++){
       for(j=0;j<i+1;j++)
               System.out.println(array[i][j]+ " ");
          System.out.println();
       }
     
        }
    }
 
The output of this program is:
run:
0

1
2

3
4
5

6
7
8
9

BUILD SUCCESSFUL (total time: 1 second)


one dimensional array:

one dimensional array:
package javaapplication27;


public class OneDArray {
    public static void main(String[] args) {
     
   
    int days[]=new int[12];//declaring array
    days[0]=1;
    days[1]=10;
    days[2]=112;
    days[3]=12;
    days[4]=13;
   
        System.err.println(days[0]);
   
}
}


The output is:
run:
1
BUILD SUCCESSFUL (total time: 1 second)

Monday, December 21, 2015

Java Program to show diamond Pattern:

Java Program to show diamond Pattern:

import java.util.Scanner;
public class MyDiamondPattern {
  public static void main(String[] args) {
    System.out
      .print("Enter the number of stars you want in diamond shape: ");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    System.out.println("Diamond of Stars are \n");
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < (n - i); j++)
        System.out.print("  ");
      for (int j = 1; j <= i; j++)
        System.out.print(" *");
      for (int k = 1; k < i; k++)
        System.out.print(" *");
      System.out.println();
    }
 
    for (int i = n - 1; i >= 1; i--) {
      for (int j = 0; j < (n - i); j++)
        System.out.print("  ");
      for (int j = 1; j <= i; j++)
        System.out.print(" *");
      for (int k = 1; k < i; k++)
        System.out.print(" *");
      System.out.println();
    }
 
    System.out.println();
  }

}
The output of this program is below:
run:
Enter the number of stars you want in diamond shape: 7
Diamond of Stars are

             *
           * * *
         * * * * *
       * * * * * * *
     * * * * * * * * *
   * * * * * * * * * * *
 * * * * * * * * * * * * *
   * * * * * * * * * * *
     * * * * * * * * *
       * * * * * * *
         * * * * *
           * * *
             *



java program to show fibonacci :


java program to show fibonacci :

public class MyFibonacci {
  public static int fibonacciRecusion(int number){
    if(number == 1 || number == 2){
      return 1;
    }
   
    return fibonacciRecusion(number-1) + fibonacciRecusion(number -2);
  }
 
  public static void main(String a[]){  
    int febCount = 50;
    int[] feb = new int[febCount];
    feb[0] = 1;
    feb[1] = 1;
   
    for(int i=2; i < febCount; i++){
      feb[i] = feb[i-1] + feb[i-2];
    }
   
    System.out.println("Fibonacci by loop and array");
    for(int i=0; i< febCount; i++){
      System.out.print(feb[i] + " ");
    }
   
    System.out.println("\nFibonacci by recusion");
    for(int i=1; i<=febCount; i++){
      System.out.print(fibonacciRecusion(i) +" ");
    }
  }
}

Java program to show Factorail Number:


Java program to show Factorail Number:

import java.util.Scanner;

class MyFactorial {
  public static void main(String args[]) {
    int n, fact = 1;
 
    System.out.println("Enter an integer to calculate it's factorial");
    Scanner in = new Scanner(System.in);  
    n = in.nextInt();
 
    if ( n < 0 ) {
      System.out.println("Number should be non-negative.");
    } else {
      for ( int c = 1 ; c <= n ; c++ ) {
        fact *= c;
      }
      System.out.println("Factorial of "+n+" is = "+fact);
    }
  }
}
The output of this program is as:
Enter an integer to calculate it's factorial
6
Factorial of 6 is = 720

java program to display diamond:

java program to display diamond:


class MyDiamond {
  public static void main(String[] args) {
    for (int i = 1; i < 10; i += 2) {
      for (int j = 0; j < 9 - i / 2; j++)
        System.out.print(" ");

      for (int j = 0; j < i; j++)
        System.out.print("*");

      System.out.print("\n");
    }
 
    for (int i = 9; i > 0; i -= 2) {
      for (int j = 0; j < 9 - i / 2; j++)
        System.out.print(" ");

      for (int j = 0; j < i; j++)
        System.out.print("*");

      System.out.print("\n");
    }
  }
}
The output of this program is as:
          *
        ***
       *****
      *******
     *********
     *********
      *******
       *****
        ***
         *

Friday, December 18, 2015

java program to demonstrate method overloading:

java program to demonstrate method overloading:
package javaapplication16;


public class MethodOverlodingDemo {
     int x;
    public void sum(){
        System.out.println("sum");
    }
    public void sum(int x){ //here method sum is overloaded by passing parameter
        this.x=x;
        System.out.println(x);
    }
    public static void main(String[] args) {
        MethodOverlodingDemo obj = new MethodOverlodingDemo();
       obj.sum();
       obj.sum(8);
       
    }
}

Sunday, December 13, 2015

java program to print FibonacciSeries

java program to print FibonacciSeries :
package pkgclass;


public class FibonaciSeries {
    public static void main(String args[])
{  
 int n1=0,n2=1,n3,i,count=10;  
 System.out.print(n1+" "+n2);  
   
 for(i=2;i<count;++i)  
 {  
  n3=n1+n2;  
  System.out.print(" "+n3);  
  n1=n2;  
  n2=n3;  
 }
}
}

Java program to Demonstrate Abstract Class and Methods

Java program to Demonstrate Abstract Class and Methods:
package abstractclassdemo;


public abstract class Animal {
    public abstract void makesound();
}

*******************

package abstractclassdemo;


public class sheep extends Animal {

    @Override
    public void makesound() {
        
        System.out.println("mooooooooooooo");
    }
    
}
***************************************

package abstractclassdemo;


public class Tiger extends Animal {
    public void makesound(){
        System.out.println("roarrrrrrrrr");
    }
    
}
***********************

package abstractclassdemo;


public class AbstractClassDemo {

    
    public static void main(String[] args) {
  AbstractClassDemo obj=new AbstractClassDemo();
     Animal tigerobj=new Tiger();
     Animal sheepobj=new sheep();
     
     obj.SpecficSound(tigerobj);
    }
    public  static void SpecficSound(Animal animal){
animal.makesound();
}
    
}

Java program to Overide toString Method:

Java program to Overide toString Method:


package overidetostringmethod;


public class Employee {
int empid;
String empName;
String empAddress;

 Employee() {
         this.empid=0;
 this.empName="default";
 this.empAddress="default";

    }
    
    public Employee(int empid,String empName,String empAddress){
    this.empid=empid;
    this.empName=empName;
    this.empAddress=empAddress;
    
    
    }

   
    
    public String toString(){
    String str= this.empid + this.empName +this.empAddress;
    return str;
    }

}
*************************************

package overidetostringmethod;
import java.util.Scanner;

public class MyClass {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        
        
        Employee obj1= new Employee();
        System.out.println("Enter id");
        int id=input.nextInt();
        
        System.out.println("Enter Name");
        String name=input.next();
        
        System.out.println("Enter Address");
        String Address=input.next();
        
        Employee obj2= new Employee(id,name,Address);
        System.out.println(obj1.toString());
    }
 
}

Java program to Demonstrate ShortCircuit:

Java program to Demonstrate ShortCircuit:

package shortcircuit;
import java.util.Scanner;

public class NewClass {
   
    public boolean getTrue(){
        System.out.println("Inside getTrue()");
        return true;
    }
        public boolean getFalse(){
            System.out.println("inside getFalse()");
            return false;
       
        }
       
        public static void main(){
        NewClass obj=new NewClass();
       
        if(obj.getTrue()||obj.getFalse()){
            System.out.println("T||F");
        }
       
        }
    }
//
package shortcircuit;


public class ShortCircuit {
    public boolean getTrue(){
        System.out.println("Inside getTrue()");
        return true;
    }
    public boolean getFalse(){
        System.out.println("Inside getFalse()");
        return false;
    }
   
    public static void main(String[] args) {
        ShortCircuit obj=new ShortCircuit();
        System.out.println("ShortCircuit:OR");
        
        if(obj.getTrue()||obj.getTrue()){
            System.out.println("T||T");
        }
         if(obj.getTrue()||obj.getFalse()){
            System.out.println("T||F");
        }
          if(obj.getFalse()||obj.getTrue()){
            System.out.println("F||T");
        }
           if(obj.getFalse()||obj.getFalse()){
            System.out.println("F||F");
        }
        
    }
    
}

Saturday, December 12, 2015

java program to find sum of each digit and repeat the process until users exit:


java program to find sum of each digit and repeat the process until users exit:

package javaapplication1;
import java.util.*;

public class SumofeachDigit {
   public static void main(String[] args){
   Scanner in=new Scanner(System.in);
 boolean quite=false;
 int choice=0;
 do{
     System.out.println("1.Enter a number to find sum of each digit");
     System.out.println("2.Quit");
   
   
     choice=in.nextInt();
   
     switch(choice){
         case 1:
             System.out.println("1.Enter a number");
             int og=in.nextInt();
             int num=og;
             int sum=0;
             int digit=0;
             int i=0;
                        if(num>0){
                        while((num!=0)){
             digit=num%10;
             sum +=digit;
             num=num/10;
             }
             System.out.println("sum of\t"+og+"="+sum);
                        }
                       
                        else{
                            System.out.println("Error");
                        }
           
            break;
         case 2:
                 quite=true;
                 break;
         default:System.out.println("Invalid");
     };
           
 
 }while(!quite);
       System.out.println("Thank u");
   }
}

Thursday, December 10, 2015

Java program to check ArmStrong Number


Java program to check ArmStrong Number:

package javaapplication1;
import java.util.*;

public class ArmStrongNumber {
    public static void main(String args[])
   {
      int n, sum = 0, og, r;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter a number to check if it is an armstrong number");    
      n = in.nextInt();

      og = n;

      while( og != 0 )
      {
         r = og%10;
         sum = sum + r*r*r;
         og = og/10;
      }

      if ( n == sum )
         System.out.println("Entered number is an armstrong number.");
      else
         System.out.println("Entered number is not an armstrong number.");        
   }
}

Wednesday, December 9, 2015

Java program to dispaly a current date:

Java program to dispaly a current date:


package javaapplication1;

import java.util.Calendar;
import java.util.GregorianCalendar;


public class DateDisplay {
     public static void main(String args[])
   {
      int day, month, year;
      int second, minute, hour;
      GregorianCalendar date = new GregorianCalendar();

      day = date.get(Calendar.DAY_OF_MONTH);
      month = date.get(Calendar.MONTH);
      year = date.get(Calendar.YEAR);

      second = date.get(Calendar.SECOND);
      minute = date.get(Calendar.MINUTE);
      hour = date.get(Calendar.HOUR);

      System.out.println("Current date is  "+day+"/"+(month+1)+"/"+year);
      System.out.println("Current time is  "+hour+" : "+minute+" : "+second);
   }
}

Tuesday, December 8, 2015

java Switch Case:

java Switch Case:

package pkgclass;

import java.util.Scanner;

public class SwitchCondition {
    public static void main(String[] args) {
       
   
    Scanner obj = new Scanner(System.in);
    int input= obj.nextInt();
   
    switch (input) {
   
        case 1:
            System.out.println("hello");break;
        case 2:
            System.out.println("hi");break;
            case 3:
            System.out.println("Namaste");break;
            default:
                System.out.println("bye");
    }
    }
}

Monday, December 7, 2015

Java IfElse ladder Program

Java IfElse ladder Program:

package javaapplication16;
import java.util.Scanner;

public class IfElseLader {
   
    public static void main(String[] args) {
        Scanner obj= new  Scanner(System.in);
        int input = obj.nextInt();
       
        if(input>=80 && input<=100)
         {
           
             System.out.println("Distinction");
         }
         else if(input<=79 && input>=60)
         {
           
             System.out.println("First Division");

         }
         else if(input<=59 && input>=40)
         {
           
             System.out.println("Second Division");
         }
         else if(input<=39 && input>=35)
         {
             System.out.println("Third Division");
         }else{
            System.out.println("fail");
         }

       
    }
   
}

Java program to demonstrate IfElse condition

Java program to demonstrate IfElse condition:
package javaapplication22;

import java.util.Scanner;
public class IfElseDEmo {
   
   
    public static void main(String[] args) {
        Scanner obj= new Scanner(System.in);
        int input = obj.nextInt();
       
        if(input>=40){
            System.out.println("pass");
        }else{
            System.out.println("Fail");
        }
       
    }
   
   
   
   
}

Sunday, December 6, 2015

java program to check whether a number is palindrome or not

java program to check whether a number is palindrome or not:

package javaapplication1;
import java.util.*;

public class PalindromeNumber {
    public static void main(String[] args){
    int num,rev=0;
  int dig;
    Scanner in=new Scanner(System.in);
         System.out.println("Enter a number to check if it is an palindrome number");
         num=in.nextInt();
       
         while(num!=0){
       
          dig = num % 10;
      rev = rev * 10 + dig;
      num = num / 10;
         }
    if(num==rev){
        System.out.println("palindrome number");
    }else{
    System.out.println(" not palindrome number");
    }
}
}

Saturday, December 5, 2015

Java program to reverse a number:

Java program to reverse a number:
package javaapplication1;
import java.util.*;
public class ReverseNumber {
 public static void main(String args[])
   {
      int n, reverse = 0;

      System.out.println("Enter the number to reverse");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();

      while( n != 0 )
      {
          reverse = reverse * 10;
          reverse = reverse + n%10;
          n = n/10;
      }

      System.out.println("Reverse of entered number is "+reverse);
   }
}

Java pogram to reverse a string

Java pogram to reverse a string:


package javaapplication1;
import java.util.Scanner;

public class ReverseString {
    public static void main(String args[])
   {
      String orig, rev = "";
      Scanner in = new Scanner(System.in);

      System.out.println("Enter a string to reverse");
      orig = in.nextLine();

      int length = orig.length();

      for ( int i = length - 1 ; i >= 0 ; i-- ){
         rev = rev+ orig.charAt(i);}

      System.out.println("Reverse of entered string is: "+rev);
   }
}

Friday, December 4, 2015