C Program to Find Largest Element of Array

Ex: Write a C program to print largest element of array. How to write a C program to find largest element of array. C program to print largest element of array.


Input from user:


Enter the limit: 12


Enter elements: 

12 

34
-1
54
7
9
16
44
32
21
19


Expected output:


Largest element is: 54






  Step by step logic of the given program:



1. Accept input (limit) from user declare integer variable say no.


2. Accept elements using for loop and store it in array of position i using arr[i]:

for(i=0;i<no;i++)
    {
     scanf("%d",&arr[i]);
    }
    
3. Assign max=a[0] it means assume first array element as a maximum.

4. After that use one more for loop and check condition using if-statement: if arr[i] is greater than max. Then assign current array element to max using: max=arr[i]:

for(i=0;i<no;i++)
    {
     if(arr[i]>max)
     {
     max=arr[i];
     }

    }


5. After that outside the block of for loop print value of max.





  Program to Find Largest Element of Array:


#include<stdio.h>
void main()
{
int arr[100];
int i,no,max;

printf("Enter the limit\n");
scanf("%d",&no);
  
    printf("Enter elements:\n");
    for(i=0;i<no;i++)
    {
    scanf("%d",&arr[i]);
    }
   
     max=arr[0];
    
    for(i=0;i<no;i++)
    {
    if(arr[i]>max)
    {
    max=arr[i];
    }
    }     
    printf("Maximum element is: %d",max);
   
}


Above program shows the following output:


How to write a C Program to Find Largest Element of Array




No comments:

Post a Comment

If you have any doubts, please discuss here...👇