In this tutorial you will learn about the C Program to Find Maximum Element in Array and its application with practical example.
C Program to Find Maximum Element in Array
In this tutorial, we will learn to create a C program that will find the maximum Element in Array using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C Programming.
- Creating and Using the user-defined function in C programming.
The Maximum value of array:-
As we all know array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. The index value of the array start’s from “0” if the array is having size 5 i.e. a[5] = { 1, 2, 3, 4, 5 } values can be stored in that array. Now the a[0] = 1 , a[1] = 2, a[2] = 3, a[3] = 4, a[4] = 5. On the array, we can do many operations with the help of the c language.
We will find the maximum size element with the help of conditional statements.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
STEP 1: START STEP 2: INITIALIZE array STEP 3: take input array size STEP 4: take input array elements STEP 5: check using statements STEP 6: PRINT "Largest element present in given array:" STEP 7: RETURN 0. STEP 8: END. |
Program:-
To find the Maximum value element from the array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> int main() { //declaring variables. int no; double arr[100]; //Taking input size of array printf("Enter the number of elements between 1 to 100): "); scanf("%d", &no); //any numner input for (int i = 0; i < no; ++i) { printf("Enter element %d: ", i + 1); scanf("%lf", &arr[i]); } // storing the largest number to arr[0] for (int i = 1; i < no; ++i) { if (arr[0] < arr[i]) { arr[0] = arr[i]; } } //printing output printf("Largest element = %.2lf", arr[0]); return 0; } |
Output:-
In the above program, we have first initialized the required variable
- arr[100] = it will hold the elements in an array.
- i = it will hold the integer value to control the array.
- no= it will hold the size of the array.
Printing output of the program to the user
Input Size.
Input elements of the array.
Calculating the maximum value.