C Program to Find Maximum Number in an Array Using Function

Problem:

Write a C program to find maximum number in an array.

Algorithm or Solution:

Here is the algorithm or solution of C program to find maximum or largest number in an array.
  1. Start
  2. Declare integer array, a variable to store a size of array and a counter variable 'i'.
  3. Accept the size of array.
  4. Accept array using for loop.
  5. Define a function which accept an array and size of array as arguments.
  6. Return the maximum number from the function defined above.
  7. Print the maximum number in an array.
  8. Stop.

C Program / Source Code:

Here is the source code of the C program to find the maximum or largest number in an array.
/* Aim: Write a C program to find the maximum or largest number in an array. */ 

#include<stdio.h>

int Maxarr(int arr[],int n); // Maxarr Function Prototype 

void main()
{
	int i,arr[5],num;


	printf("\n Enter size of array:- ");
	scanf("%d",&num);

	printf("\n Enter array elements:- ");
 
	for(i=0;i<num;i++)
		scanf("%d",&arr[i]);

	printf("\n The maximum number in the given array is %d \n \n",Maxarr(arr,num));
 
} // End of main() function

 // Maxarr Function to find maximum or largest number in an array
int Maxarr(int arr[],int n)
{
	int i,max;
	max=arr[0];
	for(i=0;i<n;i++)
	{
		if(max<arr[i])
		max=arr[i];
	}
 
	return max;
}

/* Output of above code:-

 Enter size of array:- 5

 Enter array elements(Max 5 elements):- 40 45 60 68 100

 The maximum number in the given array is 100
*/