C Program to Calculate the Quartiles

In this article, we will learn how to calculate the quartiles and implement it into C programming. Finding quartiles is a statistical study, where quartiles indicate the measures of central tendency. That belongs to measures of position.

Quartiles indicate one kind of average value of a given ungrouped data set. Quartiles can measure the average value of three positions of a given data set. They are 25% of distribution, 50% of the distribution, and 75% of the distribution.

Quartiles can be measured using the following three formulas:

calculate the quartiles

In the above formulas,

  • n = the number of observations, or the number of elements in the data set.
  • Q1 = First quartiles or 25% of distributions
  • Q2 = Second quartiles or 50% of distributions
  • Q3 = Third quartiles or 75% of distributions

Algorithm – Calculate the Quartiles

  1. At the very beginning of the program, we will take an array arr[100] for storing the data set and declare necessary variables.
  2. Ask the number of input n to be taken from the user.
  3. Loop until n and take input from the user using the scanf() function and store in the array arr[100]
  4. Sort the data set using any kind of sorting techniques (bubble sort used here).
  5. After getting the sorted data, serialize the raw data, and prepare to use it in the quartiles formula.
  6. Apply the quartiles formula for each Q1, Q2, and Q3.
  7. Finally, print it to the console display using printf() function.

Program – Calculate the Quartiles

#include <stdio.h>
int main() {
    int i, n, j, tmp, arr[100];
    double Q1, Q2, Q3;
    int R1, R2, R3;

    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]);

    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]);
    Q1 = n/4.0;
    Q2 = (2*n)/4.0;
    Q3 = (3*n)/4.0;

    R1 = n/4;
    R2 = (n*2)/4;
    R3 = (n*3)/4;

    if((Q1-R1) == 0) {
        printf("First quartiles (Q1): %d\n", arr[R1-1]);
    } else {
        float q1;
        q1 = arr[R1-1] + (Q1-R1)*((arr[R1]-arr[R1-1]));
        printf("First quartiles (Q1): %.2f\n", q1);
    if((Q2-R2) == 0) {
        printf("Second quartiles (Q2): %d\n", arr[R2-1]);
    } else {
        float q2;
        q2 = arr[R2-1] + (Q2-R2)*((arr[R2]-arr[R2-1]));
        printf("Second quartiles (Q2): %.2f\n", q2);
    if((Q3-R3) == 0) {
        printf("Third quartiles (Q3): %d\n", arr[R3-1]);
    } else {
        float q3;
        q3 = arr[R3-1] + (Q3-R3)*((arr[R3]-arr[R3-1]));
        printf("Third quartiles (Q3): %.2f\n", q3);

    return 0;
Sample InputSample Output
Enter 6 elements: 10 40 60 37 42 82Sorted data: 10 37 40 42 60 82
First quartiles (Q1): 23.50
Second quartiles (Q2): 40
Third quartiles (Q3): 51.00

NOTE: Data set must be sorted before calculating quartiles

Happy coding 🙂

Posted in