Easy Tutorial
❮ C Switch C Examples Factorial ❯

C Language Example - Determine the Maximum Value

C Language Examples

Determine the maximum value by inputting specified numbers from the user.

Example - Determine the Maximum Value

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

int main()
{
    int i, num;
    float *data;

    printf("Enter the number of elements (1 ~ 100): ");
    scanf("%d", &num);

    // Allocate memory for 'num' elements
    data = (float*) calloc(num, sizeof(float));

    if(data == NULL)
    {
        printf("Error!!! Memory not allocated.");
        exit(0);
    }

    printf("\n");

    // User input
    for(i = 0; i < num; ++i)
    {
       printf("Enter number %d: ", i + 1);
       scanf("%f", data + i);
    }

    // Loop to find the maximum value
    for(i = 1; i < num; ++i)
    {
       // If you need to find the minimum value, change < to >
       if(*data < *(data + i))
           *data = *(data + i);
    }

    printf("Maximum element = %.2f", *data);

    return 0;
}

Output result:

Enter the number of elements (1 ~ 100): 5

Enter number 1: 12
Enter number 2: 32
Enter number 3: 6
Enter number 4: 56
Enter number 5: 21
Maximum element = 56.00

C Language Examples

❮ C Switch C Examples Factorial ❯