program to find maximum number in array in java language.
Method 1->
Method 2->
public class Ved
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("enter size=");
int n =sc.nextInt();
int [] arr=new int[n];
System.out.print("enter elements of array=");
for(int i=0; i<n; i++){
arr[i]=sc.nextInt();
}
int mx=arr[0];
for(int i=1; i<n; i++){
if(mx<arr[i])
mx=arr[i];
}
System.out.println("the maximum elemnet is="+mx);
}
}
Method 3->
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("enter size=");
int n=sc.nextInt();
int[] arr=new int[n];
System.out.println("enter elements of array=");
for(int i=0; i<n; i++){
arr[i]=sc.nextInt();
}
int mx=Integer.MIN_VALUE;
// using math max function
for (int j=0; j<n; j++){
mx=Math.max(arr[j],mx);
}
System.out.println("maximum is="+mx);
}
}
■》 output - >
enter size=5
enter elements of array=
12
4
17
34
76
the maximum elemnet is=76
Comments
Post a Comment