Notice


AWT and SWING will be covered soon !

Monday, September 21, 2009

Get Sum of numbers passing a Array to a Method

 

image

import java.util.Scanner;

public class avg{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        double avg;
        int input[]=new int[5];
        System.out.print("Enter Values : ");
        for(int i=0;i<input.length;i++)
        {
            input[i]=scan.nextInt();   
        }
        avg=getsum(input);
        System.out.println("Sum is : "+avg);
    }
    public static double  getsum(int[] arr)   
    {
        double sum=0;
        for(int j=0;j<arr.length;j++)
        {
            sum=arr[j]+sum;
        }   
        return sum;
    }

}

Find Odd numbers and even

 

import java.util.Scanner;

public class odd
{
    public static void main(String args[])
    {
        Scanner scan=new Scanner(System.in);
        int[] arr;
        arr=new int[5];
        int[] arr_odd=new int[5];
        int[] arr_eve=new int[5];
        int odd=0;
        System.out.print("Enter 5 Numbers : ");
        for(int i=0;i<arr.length;i++)
        {           
            arr[i]=scan.nextInt();
            odd=arr[i]%2;
            if(odd==0)
            {
                arr_odd[i]=arr[i];
            }
            else
            {
                arr_eve[i]=arr[i];
            }
        }
            for(int j=0;j<arr_odd.length;j++)
            {
                System.out.println(arr_odd[j]);
            }
    }
}

Random Boolean Number

 

image

 

import java.util.Random;

public class bole
{
    public static void main(String args[])
    {
        Random rand=new Random();
        boolean bool;
        bool=rand.nextBoolean();
        System.out.println(bool);
    }
}

Generate a Random Number

image

 

import java.util.Random;

public class rand
{
    public static void main(String args[])
    {
        int val=0;
        Random myran = new Random();
        for(int i=0;i<5;i++)
        {
            val= myran.nextInt(5);
            System.out.println(val);
        }
    }
}