Answers for "find largest element in an array"

C++
0

Find the biggest element in the array

#include <iostream>
using namespace std;
int main()
{
    // input
    int n;
    cin >> n;
    int arr[10];
    int maxNum;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }

    // Algo
    maxNum = arr[0];
    for (int i = 0; i < n; i++)
    {
        if (maxNum < arr[i])
        {
            maxNum = arr[i];
        }
    }

    // output
    cout << maxNum;
    // for (int i = 0; i < n; i++)
    // {
    //     cout << arr[i] << " ";
    // }

    return 0;
}
Posted by: Guest on September-05-2021
0

largest element in array

import java.util.*;
public class tuf {
 
  public static void main(String args[]) {
 
    int arr1[] =  {2,5,1,3,0};
    System.out.println("The Largest element in the array is: " + sort(arr1));
   
    int arr2[] =  {8,10,5,7,9};
    System.out.println("The Largest element in the array is: " + sort(arr2));
  }
  static int sort(int arr[]) {
    Arrays.sort(arr);
    return arr[arr.length - 1];
  }
}
Posted by: Guest on May-06-2022

Code answers related to "find largest element in an array"

Browse Popular Code Answers by Language