Write a program for Linear Searching & Binary Searching Array in Java.

Program for linear searching array in core java :

Hello Friends, 

Today I will give you an example for writing a program for  Linear Search array in core java.

Code :

public class linear {
public static void main(String[] args) {
int a[]={1,4,2,6,-4,8,5};
System.out.println(“The element is at location”+Find(a,4));
System.out.println(“The element is at location”+Find(a,-4));
}
public static int Find(int[]a,int key)
{
int i;
for(i=0;i
{
if(a[i]==key)
return i+1;

}
return i-1;
}

}

Output:

The element is at location2
The element is at location5


Write a program for binary searching array in java :

Hello Friends, 

Today I will give you an example for writing a program for Binary Search array in core java.

Code :

public class binary {

public static void main(String[] args) {

int a[]={10,20,30,40,50,60};
int key=40;
Find(a,0,5,key);

}
public static void Find(int[]a,int low,int high,int key)
{
int mid;
if(low>high)
{
System.out.println(“Please Arrange elements in order…!!!”);
return;
}
mid=(low+high)/2;
if(key==a[mid])
System.out.println(“n the element is at the position=”+(mid+1));
else
if(key
Find(a,low,mid-1,key);
else
if(key>a[mid])
Find(a,mid+1,high,key);
}

}

Output :

 the element is at the position=4

How to write a program for Looping in core Java ?

Program for “while loop” in core java :

Hello Friends, 
 
Today I will give you an example for writing a program for use “while loop” in core java.
 
Code:

public class whiledemo {
public static void main(String args[])
{
int count=1,i=0;
while(count<=9)
{
i=i+1;
System.out.println(“the value of i=”+i);
count++;
}
}
}

output :
 
the value of i=1
the value of i=2
the value of i=3
the value of i=4
the value of i=5
the value of i=6
the value of i=7
the value of i=8
the value of i=9

Program for break statement in core java :

Hello Friends, 
 
Today I will give you an example for writing a program for use “break statement” in core java.
 
Code:
public class breakdemo {
public static void main(String args[])
{
for(int i=1;i<=20;i++)
{
if(i%10==0)
{
System.out.println(“Number”+i+”is divisible by 10”);
break;
}
else
{
System.out.println(“Number “+i+”is not divisible by 10”);
}
}
}
}
 
output :
 
Number 1is not divisible by 10
Number 2is not divisible by 10
Number 3is not divisible by 10
Number 4is not divisible by 10
Number 5is not divisible by 10
Number 6is not divisible by 10
Number 7is not divisible by 10
Number 8is not divisible by 10
Number 9is not divisible by 10
Number10is divisible by 10
 
 

Program for “for loop” in core java :

Hello Friends, 
 
Today I will give you an example for writing a program for use “for loop” in core java.


Code:

public class forloopdemo {

public static void main (String args[])
{
for(int i=0;i<=5;i++)
System.out.println(“The value of i=”+i);
 
}
}
output :
 
The value of i=0
The value of i=1
The value of i=2
The value of i=3
The value of i=4
The value of i=5

How to write a program for Metrix addition,subtraction & multiplication in Java.

Hello Friends, 

Today I will give you an example for writeing the java program for Metrix addition, Metrix subtraction and Metrix Multiplication.


Java code:


          import java.io.IOException;
         public class metrix {
           public static void main(String  args[]) 
    throws IOException {
      int a[][] = {{30,55,29},{54,67,84},{76,89,43}};
      int b[][] = {{4,7,5},{2,6,5},{1,4,8}};
      int c[][] = new int [3][3];
        System.out.println(“THe Matrix A:”);
               for(int i=0;i<3 font="" i="">
                 {
                    for(int j=0;j<3 font="" j="">
                     {
                       System.out.print(” “+a[i][j]);
                      }
                     System.out.println(“n”);
                  }
         System.out.println(“THe Matrix B:”);
         for(int i=0;i<3 font="" i="">
           {
            for(int j=0;j<3 font="" j="">
             {
                System.out.print(” “+b[i][j]);
             }
               System.out.println(“n”);
           }
     char choise;
        System.out.println(“Enter choise: 1)sum ,2)sub ,3)mul. “);
      choise = (char)System.in.read();
      switch(choise)
     {
         case ‘1’:
        {
                    for(int i=0;i<3 font="" i="">
                {
                    for(int j=0;j<3 font="" j="">
                    {
                        System.out.print(” “+(a[i][j]+b[i][j]));
                    }
                    System.out.println(“n”);
                }
         break;
        }
        case ‘2’:
        {
                        for(int i=0;i<3 font="" i="">
                    {
                        for(int j=0;j<3 font="" j="">
                        {
                         System.out.print(” “+(a[i][j]-b[i][j]));
                        }
                        System.out.println(“n”);
                    }
                        break;
        }
         
        case ‘3’:
        {
                   for(int i=0;i<3 font="" i="">
                    {
                        for(int j=0;j<3 font="" j="">
                        { c[i][j]=0;
                            for(int k=0;k<3 font="" k="">
                            {
                            c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
                            }
                        }
                        System.out.println(“n”);
                    }
                   System.out.println(“Multiplication is:”);
                   for(int i=0;i<3 font="" i="">
                    {
                        for(int j=0;j<3 font="" j="">
                        {   
                              System.out.print(” “+c[i][j]);
                        }
                        System.out.println(“n”);
                    }
                        break;
        }
       default:
                return;
      }
    }

      Output :
      
         




}

How to write basic Java programs & switch case Statement in Java.

Simple Java Program :

Hello Friends, 

Today I will give you an example for writing simple core java program.

Code:

public class firstprogram {
public static void main(String args[])
{
System.out.println(“This is my first java program.”);
}
}
Output :
This is my frist java program.

Java program for data type :

Hello Friends, 

Today I will give you an example for writing program for different data types in core java.

Code:

public class datatype {

public static void main(String args[])
{
byte a=10;
short  b=10*149;
int c =10000*149;
long d=10000*1000*149;
double e=99.9999;
char f=’v’;
boolean g=true;
boolean h=false;
System.out.println(“the value of a=”+a);
System.out.println(“the value of b=”+b);
System.out.println(“the value of c=”+c);
System.out.println(“the value of d=”+d);
System.out.println(“the value of e=”+e);
System.out.println(“the value of f=”+f);
f++;
System.out.println(“the value of f after increment f=”+f);
System.out.println(“the value of g=”+g);
System.out.println(“the value of h=”+h);
}
}
Output :
the value of a=10
the value of b=1490
the value of c=1490000
the value of d=1490000000
the value of e=99.9999
the value of f=v
the value of f after increment f=w
the value of g=true
the value of h=false

Java program for Escape Sequences :

Hello Friends, 

Today I will give you an example for writing program for Escape Sequences in core java.

Code:

public class Escapedemo {
public static void main(String args[])
{
System.out.println(“t This is a tab demo”);
System.out.println(“n This is new line demon”);
System.out.println(“\is printed”);
}
}

Output :
This is a tab demo

 This is new line demo

is printed

Java program for do…..while Statement :

Hello Friends, 

Today I will give you an example for writing program for “do…while “statement.

Code:

public class dowhiledemo {

public static void main (String args[])
{
int count=1,i=0;
do
{
i=i+1;
System.out.println(“The value of i=”+i);
count++;
}while(count<=8);
}
}
Output :

The value of i=1
The value of i=2
The value of i=3
The value of i=4
The value of i=5
The value of i=6
The value of i=7
The value of i=8

Java Program for switch case Statement :

Hello Friends, 

Today I will give you an example for writing program for “Switch case” in core java.

Code:

public class switchcasedemo {

public static void main (String args[])
throws java.io.IOException
{
char choice;
System.out.println(“t Program for switch case demo”);
System.out.println(“What is your sex ?”);
System.out.println(“1.male”);
System.out.println(“2.female”);
System.out.println(“3.None of the above”);
System.out.println(“Enter your choice”);
choice=(char)System.in.read();
switch(choice)
{
case’1′ : System.out.println(“You are Male.”);
break;
case’2′ : System.out.println(“You are female.”);
break;
default : System.out.println(“None of the above. :D”);
}
}
}
Output :

Program for switch case demo
What is your sex ?
1.male
2.female
3.None of the above
Enter your choice