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


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.

الأجوبة

ابحث عن مسائل برمجة جافا | Java programming بالانجليزي

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);
	}
}

القوائم الدراسية التي ينتمي لها السؤال

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!