Wednesday, April 1, 2015

java-Do while

 
public class DoWhile {
 
    public static void main(String[] args) {
        int coun=0;
        do{
            coun++;
            System.out.println(coun);
            
        }while(coun<=10);
 
    }
 
}

Number length

 
public class NumberLength {
    public static void main(String[] args) {
 
        System.out.println(average(1,2,3,4,5,6));
    }
    public static int average(int... numbers)
    {
        int total=0,a = 0;
        for (int i=0;i<numbers.length;i++)
        {
            a=numbers[i]+a;
        }
        return a;
    }
}

Inhance Loop

 
public class EnhanceLoop {
    public static void main(String[] args) {
        int a[]={5,2,3,5};
        int total =0;
        for(int x:a)
        {
            total+=x;
        }
        System.out.println(total);
    }
 
}

ARRAY in Method

 
public class ArrayMethod {
    public static void main(String[] args) {
        int a[]={3,5,4,6};
        change(a);
    }
    public static void change(int x[])
    {
        for(int i=0;i<x.length;i++)
        {
            System.out.println(x[i]);
        }
    }
}

Array Input 2D

public class Array2Dinput {
    public static void main(String[] args) {
        
        int two[][]={{8,3,4,5},{2,4,5,3,6},{4,1,2,5,7,8}};
        int one[][]={{4,2,4,5,3},{1,2,4,5,6},{5}};
        
        System.out.println("One");
        display(one);
        System.out.println("TWO");
        display(two);
    }
    public  static void display(int x[][])
    {
        for(int i=0;i<x.length;i++)
        {
            for(int j=0;j<x[i].length;j++)
            {
                System.out.print(x[i][j]+" ");
            }
            System.out.println("\t");
        }
    }
}

Java- Array–2D

Java Array 2D

 

import java.util.Scanner;
import java.util.Scanner;
 
public class Array2D {
    
    public static void main(String[] args) {
        
Scanner v = new Scanner(System.in);
int a,b;
System.out.println("Enter row :");
    a= v.nextInt();
System.out.println("Enter coloum :");
    b= v.nextInt();
    
    int c[][]=new int[a][b];    
    
    for(int i=0;i<c.length;i++)
    {
        for(int j=0;j<c[i].length;j++)
        {
            c[i][j]=v.nextInt();
        }
    }
    //............................
    System.out.println("output");
    for(int i=0;i<c.length;i++)
    {
        for(int j=0;j<c[i].length;j++)
        {
            System.out.print(c[i][j]+" ");
        }
        System.out.println("\n");    
    }
    }
}