Pages

Saturday, January 31, 2015

SEARCHING


1.) Sequential Search(Linear Searching)


class seqSearch
{
 puclic static void main (string args[]){

int x = Integer.parseInt(args[0]);

int a[x] = {                          };

int i, pas = -1;

for(i=0; i<a.length; i++)
 {
   if(a[i] == x)
    {
      pas = i;
      break;
     }
  }
if(pas == -1)

system.out.println("value not found");
else
   system.out.println("value found at index:-" +pas);
  }
}






2.) Binary Search :--


class seqSearch
{
 puclic static void main (string args[]){

int x = Integer.parseInt(args[0]);

int a[x] = {                          };

int first = 0, last = a.length-1, mid, pas = -1;

while(first<=last)
{
 mid = (first+last)/2;
if( x > a[mid])
first = mid +1;
else if(x<a[a])
 last= mid-1;
else
{
 pass= mid;
break;
 }
}











Friday, January 30, 2015

ARRAYs

ARRAYs



Declaration:-


datatype arr name[];
        or
datatype[] arrname;



MEMORY ALLOCATION :-

array name = new datatype[size]

a = new int [10]
per =  new float[20]



INITIALIZATION :-



int a[] = {3,6,4,1,8,5,9,2}



SIZE:-

n = a.length;


RUN TIME INPUT 


import java.io.*;

class runime
{
 public static void main (string args[])throws IO Exception{

 InputStreamReader input = new InputStreamReader(system.in);
 BufferedReader in = new BufferedReader(input);

 system.out.println("enter 2 numbers");
 string val = in.read lin();
 int a = Integer.parseInt(val);
 int b = Integer.parseInt(in.readLine());
 int c = a+b;
 system.out.println("sum=" +c);
 }
}




CREATING A SIZED ARRAY :--



import java.io.*;

class arr
{
 public static void main(string args[])throws IO Exception{
BufferReader in = new BufferReader(new ISR(system.in));

int n,i;

system.out.println("enter size of array");
n = Integer.parseInt(in.readLIne());

int a[] = new int[n];

for(i=0; i<a.length; i++);
 {
  system.out.println("enter value");
  a{i] = Integer.parseInt(in.readLine);
 }

system.out.println("Array");

int s = 0;

for(i=0; i<n; i++)
 {
   system.out.println(a[i] +"\t");
   s = s+a[i];
 }

system.out.println("\n sum =" +s);

float avg = (float) s/n;
system.out.println("Avg =" +avg);
 }
}








COUNT EVEN OR ODD ELEMENTs :--







class arr
{
 public static void main(string args[])throws IO Exception{

BufferReader in = new BufferReader(new ISR(system.in));

int n,i;

system.out.println("enter size of array");
n = Integer.parseInt(in.readLIne());

int a[] = new int[n];

for(i=0; i<a.length; i++);
 {
  system.out.println("enter value");
  a{i] = Integer.parseInt(in.readLine);
 }

system.out.println("Array");

int even = 0;
int odd = 0;

for(i=0; i<n; i++)
{
system.out.println( a[i] + "\t");
 if(a[a]%2==0)
    even++;
else
    odd++;
 }

system.out.println("Even elements =" +even);

system.out.println("Odd elements =" +odd);
  }
}




REVERSE OF ARRAY:--



class ArrRev
{
 public static void main(srting args[]){

int a[] = {                         };
int i.j.t;
system.out.println("Array");

for(i=0; i<a.length; i++)
 {
  system.out.println(a[i] +"\t");
 }

for(i=0, j= a.length-1; i<a.length/2; i++, j--)

 {
  t = a[i];
  a[i] = a[j];
  a[j] = t;
 }

system.out.println("Reversed Array");

for(i=0; i=a.length; i++)
   {
     system.out.println(a[i] + "\t");
    }
  }
}


















Thursday, January 29, 2015

LOOPs

LOOPs


1.) for loop

2.) while loop

3.) do while loop



FIRSTLY WE ARE GOING TO TALK ABOUT FOR LOOP 

In programming language FOR loop is used to allow code executed repeatedly. For loop is useful when you know how many times a task to be repeatedly.

example :-
       
                 for(exp1; exp2; exp3)
                      {
                         statements;
                      }

where : 
    
     exp1 :- initialization expression 
     exp2 :- test expression
     exp3 :- update expression.


EXAMPLE FOR CHECK WEATHER NUMBER IS PRIME OR NOT


 class prime 
{
 public static void main(string args[]){

int n = Integer.parseInt(args[0]);
boolean flag = true;
for(int i=2; i<n/2; i++)
 {
   if(n%i==0)
    {
      d=i;
      flag = false
      break;
     }
  }
if(flag)
system.out.println("number is prime");
else
system.out.println("number is not prime");
  }
}



NESTED LOOP



The placing of one loop inside the body of another loop is called nesting. The most common nested loop are for loop.

Example of nested loop a table up to n numbers :-


class table
{
 public static void main(string args[]){

for(int i=1; i<=10; i++)
  {
    for(int j=1; j<=10; j++)
    {
      system.out.println(i + "" + j);
    }
  }
}




WHILE LOOP




In programming language WHILE loop is used to control flow statement that allow code to repeatedly executed based on the condition.


example :-

              while(condition)
                   {
                    statements
                    ________
                     ________
                    }



Example for sum of digits


class digitSum

{
  public static void main(string args[]){

  int n = Integer.parseInt(args[0]);
  int r,s = 0;
  while (n!=0)
    {
      r = n%10;
      s = s+r;
      n = n/10;
    }
system.out.println("sum of digits="+s);

 }
}




DO WHILE LOOP


In programming language DO WHILE loop is used to control flow statement that executes a part of code single time and then repeatedly executes the part of code, its depend on the giving condition in the program. 


example:-

        do
            {
              statements;
                _______
                _______
             }
         while(condition)


Example to print number from 1 to 19


class example
{
 public static void main(string args[]){

int x= 1;

do
  {
    system.out.println("x is :=" +x);
    x++;
   system.out.println("/n");
   }
 while(x=<20);
 }
}















SWITCH CASE 

switch (exp)
{
 case constant 1;
          statement;
         break;
  case constant 2;
           statement;
           break;
_____________
           ________
          ________
          default;
        statement;
}

RULES FOR SWITCH CASE :=

1.)  CASE CONSTANTS MUST BE OF TYPE INT OR CHAR.

2.) NO TWO CASE MAY BE SAME.

3.) BREAK IS OPTIMAL.

4.) DEFAULT IS OPTIMAL.


EXAMPLE


class color 
{
 public static void main (string args[])
 {
  char ch = args[0.].char At(0);

  switch(ch)
   {
     case "R":
     system.out.println("RED");
     break;
     case "G":
     system.out.println("GREEN");
     break;
     case "B":
     system.out.println("BLUE");
     break;
    default;
     system.out.println("UNDEFINED"); 
   } 
  }
 }


AS SAME WE CAN MAKE A CALCULATOR OR MORE OTHER PROGRAMS.














Saturday, January 17, 2015

A Example of "if else" loop

Here i'm going to give you an example of of "if else" loop to write a program on 

FIND THE TOTAL MARKS AND PERCENTAGE OF AA STUDENT IN 5 SUBJECTS 



class student 

{
  public static void main(string args[]);

  {
    int a,b,c,d,e,t;  
    float p;

a= Integer.parseInt(args[0]);
b= Integer.parseInt(args[1]);
c= Integer.parseInt(args[2]);
d= Integer.parseInt(args[3]);
e= Integer.parseInt(args[4]);

t=(a+b+c+d+e);

system.out.println("total marks are" +t);
p=(t/500)*100;
system.out.println("percentage is " +p);
if (p>=4)
{
system.out.println("student is passed");
}
else
 {
   system.out.println("student failed"); 
 }
}
}