Sunday, 5 November 2017

Java program to creates an array of 10 elements and pass it to a function to store some even values and then display arrays elements.

Passing of array java program:

Code Screenshots:


Ouput ScreenShots :






Code of Java program creates an array of 10 elements and pass it to a function to store some even values and then display arrays elements.

Source Code :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package arrayexample1;

/**
 *

 * @author selectivetopics.blogspot.com

 */
public class ArrayExample1 {

    //this function receive an array from main function and
//stores some even values into array
    
public static void populate_Array(int[] arr){
      
      //Assign even values to each array elements 2-20
      for ( int counter = 0; counter < arr.length; counter++ )
          arr[counter]=2+counter*2 ;
        
    }
    
    public static void main( String[] args )
   {
      int[] array; // declare array named array

      array = new int[ 10 ]; // create the array object
      populate_Array(array);
      System.out.printf( "%s %8s\n", "Index", "Value" ); // column headings
     // output each array element's value 
      for ( int counter = 0; counter < array.length; counter++ )
         System.out.printf( "%5d %8d\n", counter, array[ counter ] );
   }

    
}



No comments:

Post a Comment