0% found this document useful (0 votes)
7 views3 pages

Calculate Mean, Median, Mode in C

The document contains a C program that calculates the mean, median, and mode of a given array of integers. It prompts the user to input the number of elements and the elements themselves, then processes the data to compute the statistics. The results are printed to the console in a formatted manner.

Uploaded by

dasaisha419
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Calculate Mean, Median, Mode in C

The document contains a C program that calculates the mean, median, and mode of a given array of integers. It prompts the user to input the number of elements and the elements themselves, then processes the data to compute the statistics. The results are printed to the console in a formatted manner.

Uploaded by

dasaisha419
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

void findMeanMedianMode(int arr[], int n) {

// Calculate mean

int sum = 0;

for (int i = 0; i < n; i++) {

sum += arr[i];

float mean = (float)sum / n;

// Calculate median

int temp;

for (int i = 0; i < n - 1; i++) {

for (int j = i + 1; j < n; j++) {

if (arr[i] > arr[j]) {

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

float median;

if (n % 2 == 0) {

median = (arr[n / 2 - 1] + arr[n / 2]) / 2.0;

} else {

median = arr[n / 2];

// Calculate mode

int mode = arr[0];

int count = 1;
int maxCount = 1;

for (int i = 1; i < n; i++) {

if (arr[i] == arr[i - 1]) {

count++;

} else {

if (count > maxCount) {

maxCount = count;

mode = arr[i - 1];

count = 1;

printf("Mean: %.2f\n", mean);

printf("Median: %.2f\n", median);

printf("Mode: %d\n", mode);

int main() {

int n;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

findMeanMedianMode(arr, n);
return 0;

You might also like