Adsense 728 * 15

Search

Custom Search

C Program to Find Highest Number in an Array

Write a c program to find highest number in an array. Given an input array, we have to write a code to find highest number in an array.

There are multiple approaches we can use to solve this problem.

i) Find highest number in an array using sorting algorithm.

We can use any sorting algorithm such as Bubble sort, Selection sort, Insertion sort, Quick sort, Merge sort etc. to sort an array. After sorting an array element at n-1 position is highest number in an array.

The time complexity of sorting an array is O(nlogn).

ii) Traverse an array to find highest number in array.

In this approach, first declare a variable highest and assign first element of an array. After that traverse an array, and compare each element of an array with the value of highest. If any element is greater than the highest then assign that value into highest variable.

The time complexity is O(n).

Let's implement the second approach.

C++ program to find largest number in an array

C Program to Find Highest Number in an Array


C Program to Find Highest Number in an Array


In this programming example, we first take an input array from user. Then we traverse an array to find highest number in an array.

#include <stdio.h>

int main() {
    
    int arr[100], n, i, highest;
    
    printf("Enter the size of an array( Max 100) \n");
    scanf("%d", &n);
    
    printf("Enter array elements \n");
    
    //Take input values
    
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    
    
    //Assign first value 
    
    highest = arr[0];
    
    //Traverse an array
    
    for(i = 0; i < n; i++) {
        
        //If any value is greater than highest
        
        if(arr[i] > highest) {
            highest = arr[i];
        }
    }
    
    //Print largest number
    
    printf("Largest number is %d ", highest);
    
    return 0;
}



Output :

Enter the size of an array( Max 100)  5

Enter array elements
3
8
2
7
1

 Largest number is 8


Programming questions on arrays

C program to check whether a character is vowel or consonant

C program to find second smallest number in an array without sorting

No comments:

Post a Comment