Pages

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"); 
 }
}
}










Thursday, June 26, 2014

Capture image from camera in android


To capture image from android first fire an intent.
       
        private static final int REQUEST_CODE_IMAGE = 101;

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_CODE_IMAGE);


//  get data on on Activity Result use the below code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
 if (requestCode == REQUEST_CODE_IMAGE && resultCode == RESULT_OK) {

   if (data != null) {
    if (data.getExtras().get("data") != null) {
 
     Uri fileUri = data.getData();
     String filePath = getRealPathFromURI(fileUri,
       MediaStore.Images.Media.DATA);
     File file = new File(filePath);
     String fileName = file.getName();


     try {
      Bitmap photo = (Bitmap) data.getExtras().get("data");
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      photo.compress(Bitmap.CompressFormat.PNG, 100, stream);



     } catch (Exception ex) {

     }
    }
   }
  }

Show or Read PDF in Android


To Show PDF file in your android phone first save it in your assets folder than read PDF file by using below two methods.
These methods first store your pdf in local storage than show it from local storage.

private void CopyReadAssets(String fileName) {
AssetManager assetManager = getAssets();

InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), fileName);
try {
in = assetManager.open(fileName);
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/" + fileName),
"application/pdf");

startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}