How to find the Largest Number in an Array C Programming Example

Algorithm


  1. Get the array size length
  2. Loop through the array upto array size given
    1. Get the numbers and assign it to array using character index from 0
  3. Loop through again the array upto array size given
    1. If Current value (array[character]) is greater than the previous Array found location
    2. Then update found location = current character location

 

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <stdio.h>

int main()
{
    int array[1000], arraySize, character, foundLocation = 0;
    
    printf("How many numbers would be in the Array: \n");
    scanf("%d", &arraySize);

    printf("Enter %d numbers one by one: \n", arraySize);

    // Get the numbers and assign it to array using character index from 0
    for (character = 0; character < arraySize; character++) {
        scanf("%d", &array[character]);
    }

    // Loop through the array upto array size given
    for (character = 1; character < arraySize; character++) {
        
        // If Current value (array[character]) is greater than the previous Array found location, 
        // Then update found location = current character location
        if (array[character] > array[foundLocation]) {
            foundLocation = character;
        }
    }

    printf("Maximum Number Found Location: %d\n", foundLocation+1);
    printf("Maximum Number Value: %d\n", array[foundLocation]);
  
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
How many numbers would be in the Array: 3
23 199 12

Output

x
+
cmd
Maximum Number Found Location: 2
Maximum Number Value: 199

#2 How to find the Largest Number in an Array using Dynamic Memory Allocation

Code - C Programming

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double *array;
    int arraySize, character, foundLocation = 0;
    
     // Allocating memory for array Size
     array = (double *)calloc(arraySize, sizeof(double));
     
     // Error if memory is not allocated
     if (array == NULL) {
        printf("Error!!! memory not allocated.");
        exit(0);
     }
    
    printf("How many numbers would be in the Array: \n");
    scanf("%d", &arraySize);

    printf("Enter %d numbers one by one: \n", arraySize);

    // Get the numbers and assign it to array using character index from 0
    for (character = 0; character < arraySize; character++) {
        scanf("%d", &array[character]);
    }

    // Loop through the array upto array size given
    for (character = 1; character < arraySize; character++) {
        
        // If Current value (array[character]) is greater than the previous Array found location, 
        // Then update found location = current character location
        if (*array < *(array + character)) {
          *array = *(array + character);
        }
    }

    printf("Maximum Number: %d\n", *array);
    
    // As no longer needed memory, just free it.
    free(array);
  
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
How many numbers would be in the Array: 4
10 20 30 40

Output

x
+
cmd
Maximum Number:40

#3 Find Maximum Number using Function in C Programming Example

Code - C Programming

#include <stdio.h>

int findLargestNumber(int array[], int arraySize) {
    int character, foundCharacter, foundLocation = 0;
    
    for (character = 1; character < arraySize; character++) {
        if (array[character] > array[foundLocation]) {
            foundLocation  = character;
            foundCharacter = array[character];
        }
    }
    
    return foundCharacter;
}

int main()
{
    int array[1000], arraySize, character, foundLocation = 0;
    
    printf("How many numbers would be in the Array: \n");
    scanf("%d", &arraySize);

    printf("Enter %d numbers one by one: \n", arraySize);

    for (character = 0; character < arraySize; character++) {
        scanf("%d", &array[character]);
    }

    int foundValue = findLargestNumber(array, arraySize);

    printf("Maximum Number Value: %d\n", foundValue);
  
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
How many numbers would be in the Array: 3
10 20 30

Output

x
+
cmd
Maximum Number Value: 30

Demonstration


We've showed the algorithm above how  to find the largest number of an array.

Ways we've showed how to find the largest number

  1. Find Largest Number using Normal for loop
  2. Find Largest Number using Dynamic Memory Allocation
  3. Find Largest Number using Function

 

If you have any confusion. Please try at here - https://devsenv.com/codes/run/code