0
Q:

Find the maximum number of an array js

var arr = [1, 2, 3];
var max = arr.reduce(function(a, b) {
  return Math.max(a, b);
});
2
#include <stdio.h>
int main() {
    int i, n;
    float arr[100];
    printf("Enter the number of elements (1 to 100): ");
    scanf("%d", &n);

    for (i = 0; i < n; ++i) {
        printf("Enter number%d: ", i + 1);
        scanf("%f", &arr[i]);
    }

    // storing the largest number to arr[0]
    for (i = 1; i < n; ++i) {
        if (arr[0] < arr[i])
            arr[0] = arr[i];
    }

    printf("Largest element = %.2f", arr[0]);

    return 0;
}
5
---without sort method---
public static int maxValue( int[]  n ) {

int max = Integer.MIN_VALUE;

for(int each: n)

if(each > max)

max = each;

 
return max;
  
---with sort method---
public static int maxValue( int[]  n ) {

Arrays.sort( n );

return  n [ n.lenth-1 ];

}
1
function getMaxOfArray(numArray) {
    return Math.max.apply(null, numArray);
}
1
Math.max(10, 20);   //  20
Math.max(-10, -20); // -10
Math.max(-10, 20);  //  20
0

New to Communities?

Join the community