-->

Tuesday, August 18, 2015

java program for bassic array operations

/*
2. Set up an array to hold the following values, and in this order: 23, 6, 47, 35, 2, 14.
a. Write a program to get the average of all 6 numbers.
b. Using the above values, have your program print out the highest number in the array.
c. Have your program print out only the odd numbers.
d. Sort the array and print.
e. Change the program in such a way that the array values are taken from the keyboard until the user enters the number ‘0’.
*/
//08/03/2015 11:50:16 AM CLINTON.M.U//
//clintonmu.blogspot.com

import java.util.Scanner;
import java.util.Arrays;
class Array_comp_q2_l2
{
    public static void main(String args [])
    {
        int[] array1 = {23,6,47,35,2,14};
        float average;
        int biggest=0,smallest=0;
        average= (array1[0]+array1[1]+array1[2]+array1[3]+array1[4]+array1[5])/(array1.length);
        System.out.println("average is ="+average);
        for(int i=1; i< 5; i++)
                {
                        if(array1[i] > biggest)
                                biggest = array1[i];
                        else if (array1[i] < smallest)
                                smallest = array1[i];
                     
                }
        System.out.println("biggest no is "+biggest);

        for(int i=1; i< 5; i++)
                {
            if(array1[i]%2 == 0)
                                System.out.println(array1[i] + " is even number.");
                             else
                                System.out.println(array1[i] + " is odd number.");    
        }  
        Arrays.sort(array1);
            // let us print all the elements available in list
                System.out.println("The sorted int array is:");
                for (int number : array1)
                {
                System.out.println(number);
                }
       }
}

No comments:

Post a Comment

JavaScript Free Code