C Program to Calculate the Percentiles

In this article, we will learn how to calculate the percentiles and implement it into C programming. Percentile is a mathematical study in statistics. It indicates the average value for a specific position or location in an ungrouped data set.

The term percentiles come from percentages. Because in the percentiles method, a data set is divided into one hundred equals parts. And makes 99 breakpoints. The breakpoint itself does not indicate the average value. It indicates the positional values.

Percentiles are labeled as P1, P2, P3, … P99, and each percentile holds 1%, 2%, 3%, … 99% of data distributions.

percentiles measures of position

Use the following formulas to calculate the percentiles;

percentiles calculations

Where;

  • n = the number of observations or the number of elements in the data set.
  • P1 = the first percentile or 1% of data distribution
  • P2 = the second percentile or 2% of data distributions
  • P1 = the third percentile or 3% of data distributions
  • … P99 = the 99th percentile or 99% of data distributions

Algorithm – Calculate the Percentiles

  1. Declare an array of enough size arr[100] for storing the data set.
  2. Declare the necessary variables.
  3. Ask the user for data input up to nusing loops and scanf() function
  4. Sort the data set if it is unsorted using any sorting algorithm.
  5. Process the raw data from the data set and prepare to use in the percentile formula
  6. Apply the percentile formula in a loop to find all the percentile value.
  7. Display the calculated percentiles value in the console using printf() function.

Program – Calculate the Percentiles

#include <stdio.h>
int main() {
    int i, n, j, tmp, arr[100], r[99];
    double p[99];

    printf("Number of data in the data set: ");
    scanf("%d", &n);

    printf("Enter %d elements: ", n);
    for(i=0; i<n; i++) {
        scanf("%d", &arr[i]);
    }

    //using bubble sort
    for(i=0; i<n; i++) {
        for(j=0; j<n-i-1; j++) {
            if(arr[j] > arr[j+1]) {
                tmp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = tmp;
            }
        }
    }

    printf("\nSorted data: ");
    for(i=0; i<n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    for(i=1; i<=99; i++) {
        p[i] = (n*i)/100.0;
        r[i] = (n*i)/100;
    }

    for(i=1; i<=99; i++) {
        if(p[i]-r[i] == 0) {
            printf("%dth percentile(P%d): %d", i, i, arr[r[i]-1]);
        } else {
            float p1 = arr[r[i]-1] + (p[i]-r[i])*((arr[r[i]]-arr[r[i]-1]));
            printf("%dth percentile(P%d): %.2f", i, i, p1);
        }
        printf("\n");
    }

    return 0;
}
Sample InputSample Output
Number of data in the data set: 9
Enter 9 elements: 10 40 60 37 42 82 76 83 56
Sorted data: 10 37 40 42 56 60 76 82 83
1th percentile(P1): 8.18
2th percentile(P2): 8.36
3th percentile(P3): 8.54
4th percentile(P4): 8.72
5th percentile(P5): 8.90
6th percentile(P6): 9.08
7th percentile(P7): 9.26
8th percentile(P8): 9.44
9th percentile(P9): 9.62
10th percentile(P10): 9.80
11th percentile(P11): 9.98
….
97th percentile(P97): 82.73
98th percentile(P98): 82.82
99th percentile(P99): 82.91

NOTE: Percentile is only applicable for ungrouped and sorted data. Unsorted data must be sorted before calculations.

Happy coding 🙂

Posted in