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.

الأجوبة

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);
	}
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...