Largest Element of Array

Largest Element Of  Array

An array element which has the highest numerical value among all the given elements in an array is referred to as the largest element of the array.

Largest Element of Array

Syntax

[arr_size]={value 1, value 2, value 3,…,value n-1};

Algorithm

  • Step 1: Start
  • Step 2: Initialize arr[]
  • Step 3: Length = sizeof(arr)/sizeof(arr[0])
  • Step 4: Large = arr[0]
  • Step 5: Set i=0
  • Step 6: Repeat Step 7 and 8 till i < length of an array
  • Step 7: If(arr[i] > Large) Large = arr[i]
  • Step 8: i = i + 1
  • Step 9: Print the Largest element from an array
  • Step 10: Return 0
  • Step 11: End

Working Of Program

In all these following programs, we’ll learn to display the largest numbers or elements among all the given elements from the array.

Problem 1

Write a program using an array for displaying the largest element from the array.

  • Firstly, enter the number of elements of an array.
  • Then enter all the elements of an array.

Code

Run
#include<stdio.h>
int main ()
{
  int n;
  double arr[1000];
  printf ("Enter the number of elements (1 to 1000)= ");
  scanf ("%d", &n);
  for (int i = 0; i < n; ++i)
    {
      printf ("Enter number %d = ", i + 1);
      scanf ("%lf", &arr[i]);
    }
  for (int i = 1; i < n; ++i)
    {
      if (arr[0] < arr[i])
	{
	  arr[0] = arr[i];
	}
    }
  printf ("Largest element = %.2lf", arr[0]);
  return 0;
}

Output

Enter the number of elements (1 to 1000)= 6
Enter number 1 = 67
Enter number 2 = 90
Enter number 3 = 89
Enter number 4 = 679
Enter number 5 = 88
Enter number 6 = 999
Largest element = 999.00

Problem 2

Write a program using an array for displaying the largest element from the given array.

Code

Run
#include<stdio.h>
int largest (int arr[], int n)
{
  int i;
  int large = arr[0];
  for (i = 1; i < n; i++)
    if (arr[i] > large)
      large = arr[i];
  return large;
}
int main ()
{
  int arr[] =
    { 26078, 436, 904, 23, 75, 936, 777, 684, 927, 2, 8967, 5467, 34 };
  int n = sizeof (arr) / sizeof (arr[0]);
  printf ("Largest element in given array = %d", largest (arr, n));
  return 0;
}

Output

Largest element in given array = 26078

Problem 3

Write a program using function to find the largest number or element of an array.

Code

Run
#include<stdio.h>
int max (int arr[], int size)
{
  int max = arr[0];
  for (int i = 1; i < size; i++) { if (arr[i] > max)
	max = arr[i];
    }
  return max;
}

int main ()
{
  int size;
  printf ("Enter Size of Array = ");
  scanf ("%d", &size);
  int arr[size];
  printf ("Enter Array Elements = ");
  for (int i = 0; i < size; i++)
  scanf ("%d", &arr[i]);
  printf ("Largest Element = %d", max (arr, size));
  return 0;
}

Output

Enter Size of Array = 5
Enter Array Elements = 11
22
33
44
55
Largest Element = 55

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription