C program to find largest number in an array
In this post, you will learn how to find the largest number in an array using the C programming language.
Such a type of logical programming can be asked in a programming interview. It helps to improve the thinking power of a new developer. An array is a data structure representing an ordered collection of indexed items. An array is used when there is a requirement to add more items in a single variable. In the development process, we may come to the situation where we need to find the largest number in an array. Java provides various approaches to achieve this task. Here, we have mentioned most of them.
C Program to find largest element in an array using loop
In this approach, we first assume that the first element is the largest. After that, we traverse the array and if the current element is greater than the largest element, we update the largest element.
/*
* C Program to find the largest number
* in an array using loops
*/
#include <stdio.h>
int main()
{
int size, i, max;
//Initialize array
printf("\nEnter the size of array: ");
scanf("%d", &size);
int array[size];
printf("\nEnter %d elements of the array: \n", size);
for (i = 0; i < size; i++)
{
scanf(" %d", &array[i]);
}
//Initialize max with first element of array.
max = array[0];
//Loop through the array
for (i = 1; i < size; i++)
{
if (max < array[i])
max = array[i];
}
printf("\nLargest element of the given array: %d", max);
return 0;
}
Output of the above code:
Enter the size of array: 5
Enter 5 elements of the array:
89
34
53
62
5
Largest element of the given array: 89
Largest Element of an Array in C using Recursion
Here is the source code of the C Program to find the largest number in an array using recursion.
#include <stdio.h>
#include <math.h>
// Function to get max of two numbers
int max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
// Function to get largest number
int largest(int arr[],int size,int i)
{
if(i == size-1)
return arr[i];
return max(arr[i], largest(arr,size,i+1));
}
int main()
{
int size;
printf("Enter Size of Array: ");
scanf("%d",&size);
//Initialize array
int arr[size];
//Input array elements
printf("Enter Array Elements: ");
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Largest element of the given array: %d",largest(arr,size,0));
return 0;
}
Output of the above code:
Enter Size of Array: 6
Enter Array Elements: 89 34 23 95 63 74
Largest element of the given array: 95
Largest Element of an Array in C using pointer
In the given C Program, we find the largest number in an array using pointer. We ask the user to provide the size of the array and the elements of the array. We traverse the complete array and if the current element is greater than the largest element, we update the largest element pointer. At first, we assume that the first element is the largest. After that, we traverse the array and if the current element is greater than the largest element, we update the largest element.
#include <stdio.h>
int main()
{
long arr[100], *max, size, x, loc = 1;
printf("Enter the size of array: ");
scanf("%ld", &size);
printf("\nEnter %d elements of the array: ", size);
for ( x = 0 ; x < size ; x++ )
scanf("%ld", &arr[x]);
max = arr;
*max = *arr;
for (x = 1; x < size; x++)
{
if (*(arr+x) > *max)
{
*max = *(arr+x);
loc = x+1;
}
}
printf("\nLargest element of the given array: %ld", *max);
return 0;
}
Output of the above code:
Enter the size of array: 5
Enter 5 elements of the array: 89 54 23 94 61
Largest element of the given array: 94
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative