Q:

Write a java program that declares an array of 10 integers. Then, find and print the maximum number in the array

0

Write a java program that declares an array of 10 integers. Then, find and print the maximum number in the array.

---------

Re-write the program by developing a method to find the maximum number in the array, by passing the array to the method that will handle task. In the main, you have to declare the array and then call method and pass the array to the method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

part 1:

public class Main
{
	public static void main(String[] args) {
	    int a[]={3,5,1,7,5,2,54,77,2,45};
	    int max=a[0];
	     for(int i=0; i<a.length;i++)
        	{
        	  if(a[i]>max) 
        	  max=a[i];
        
        	}
        	
	  System.out.println("highest number in the array is "+max);
}
}

 

part 2 (using a method):

public class Main
{
	public static void main(String[] args) {
	    int a[]={3,5,1,7,5,2,54,77,2,45};
	  maximum(a[]);      
	    }
	
	public static void maximum(int []a)
	{
	    int max=a[0];
	     for(int i=0; i<a.length;i++)
        	{
        	  if(a[i]>max) 
        	  max=a[i];
        
        	}
        	
	  System.out.println("highest number in the array is "+max);
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now