C Program to Find Maximum Number in an Array
Problem: Write a C program to find maximum or largest element or number in an array. Write a C program to print the index or location of maximum element in an array.
Solution: To find the maximum number in an array we first assume the first element as maximum number. Then we compare the next element with current maximum and if it is greater we set it as maximum. We continue this iterations until array is finished. Similarly we can find the minimum element in an array.
The time complexity of this algorithm is O(n) because time required to find maximum is directly proportional to the input size of the array.
Solution: To find the maximum number in an array we first assume the first element as maximum number. Then we compare the next element with current maximum and if it is greater we set it as maximum. We continue this iterations until array is finished. Similarly we can find the minimum element in an array.
The time complexity of this algorithm is O(n) because time required to find maximum is directly proportional to the input size of the array.
Algorithm to find largest number in an array
- Start
- Declare integer array, a variable to store largest or maximum number in an array
- Accept array elements using for loop.
- Initialize max=arr[0], first element of array.
- Use for loop, check every time if next element is greater than max , if yes assign that element to max otherwise continue.
- Print largest element max.
- Stop.
C program to find maximum or largest number in an array
/* Aim: C program to find maximum or largest number in array */
#include<stdio.h>
int main()
{
int i,arr[100],maximum,size,index;
printf("\n Enter size of array:- ");
scanf("%d",&size);
printf("\n Enter array elements:- ");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
maximum=arr[0];
for(i=0;i<size;i++)
{
if(maximum<arr[i])
{
maximum=arr[i];
index=i;
}
}
printf("\n The maximum number %d is present at %d th location in the given array. \n \n",maximum,index+1);
return 0;
}
Output of C Program:
C programming code to find maximum number in an array using function
/* Aim: Write a C program to find the maximum or largest number in an array using function */
#include<stdio.h>
int Maximum(int arr[],int n); // Maximum() Function Prototype
int main()
{
int i,arr[10],size,index;
printf("\n Enter size of array:- ");
scanf("%d",&size);
printf("\n Enter array elements:- ");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
index=Maximum(arr,size);
printf("\n The maximum number %d is present at %d th location in the given array. \n \n",arr[index],index+1);
return 0;
} // End of main() function
// Maximum() Function to find maximum or largest number in an array
int Maximum(int arr[],int n)
{
int i,max,index;
max=arr[0];
for(i=0;i<n;i++)
{
if(max<arr[i])
{
max=arr[i];
index=i;
}
}
return index;
}
/* Output of above code:-
Enter size of array:- 5
Enter array elements:- 101 90 99 89 95
The maximum number 101 is present at 1 th location in the given array
*/
C program to find maximum and minimum number in an array using pointers
/* Aim: C program to find maximum and minimum number in an array using pointers */
#include<stdio.h>
int main()
{
int i,arr[10],maximum,minimum,size,maximum_Index,minimum_Index;
printf("\n Enter the size of array:- ");
scanf("%d",&size);
printf("\n Enter array elements:- ");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
maximum=arr[0];
minimum=arr[0];
for(i=0;i<size;i++)
{
if(maximum<*(arr+i))
{
maximum=*(arr+i);
maximum_Index=i;
}
if(minimum>*(arr+i))
{
minimum=*(arr+i);
minimum_Index=i;
}
}
printf("\n The maximum number %d is present at %d th location in the given array \n",maximum,maximum_Index+1);
printf(" The minimum number %d is present at %d th location in the given array \n \n",minimum,minimum_Index+1);
return 0;
}
/* Output of above code:-
Enter size of array:- 5
Enter array elements:- 70 99 45 80 98
The maximum number 99 is present at 2 th location in the given array
The minimum number 45 is present at 3 th location in the given array
*/