C program to find maximum between three numbers

List Topics
July 7, 2024
No Comments
7 min read

A simple C program to find the maximum of three numbers with an explanation of each part of the code.

C
#include <stdio.h>

int main() {
    int num1, num2, num3, max;

    // Asking for user input
    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    // Finding the maximum number
    if (num1 > num2) {
        if (num1 > num3) {
            max = num1;
        } else {
            max = num3;
        }
    } else {
        if (num2 > num3) {
            max = num2;
        } else {
            max = num3;
        }
    }

    // Printing the maximum number
    printf("The maximum number is: %d\n", max);

    return 0;
}

Explanation:

  1. Header File Inclusion:
C
#include <stdio.h>

This line includes the standard input-output library which is necessary for using printf and scanf functions.

2. Main Function:

C
int main() {

This is the entry point of the C program where execution begins.

3. Variable Declaration:

C
int num1, num2, num3, max;

4. User Input:

C
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
  • printf is used to display a message asking the user to enter three numbers.scanf is used to read
  • the input numbers and store them in the variables num1, num2, and num3.

5. Finding the Maximum Number:

C
if (num1 > num2) {
    if (num1 > num3) {
        max = num1;
    } else {
        max = num3;
    }
} else {
    if (num2 > num3) {
        max = num2;
    } else {
        max = num3;
    }
}
  • The first if statement checks if num1 is greater than num2.
  • If true, the nested if statement checks if num1 is also greater than num3. If true, num1 is the maximum. Otherwise, num3 is the maximum.
  • If num1 is not greater than num2, the else block checks if num2 is greater than num3. If true, num2 is the maximum. Otherwise, num3 is the maximum.

6. Printing the Maximum Number:

C
printf("The maximum number is: %d\n", max);

This line prints the maximum number stored in the variable max.

7. Return Statement:

C
return 0;

This statement indicates that the program executed successfully and returns 0 to the calling process.


C Program (Using Ternary Operator):

C
#include <stdio.h>

int main() {
    int num1, num2, num3, max;

    // Asking for user input
    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    // Finding the maximum number using ternary operator
    max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);

    // Printing the maximum number
    printf("The maximum number is: %d\n", max);

    return 0;
}

Explanation:

  1. Finding the Maximum Number Using Ternary Operator:
C
max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
  • The ternary operator ? : is a compact form of an if-else statement.
  • The expression (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3) evaluates as follows:

> If num1 is greater than num2, it further checks if num1 is greater than num3. If true, num1 is assigned to max. Otherwise, num3 is assigned to max.

> If num1 is not greater than num2, it checks if num2 is greater than num3. If true, num2 is assigned to max. Otherwise, num3 is assigned to max.

2. Printing the Maximum Number:

C
printf("The maximum number is: %d\n", max);

This line prints the maximum number stored in the variable max.


C Program (Using a Function ):

C
#include <stdio.h>

// Function prototype
int findMax(int a, int b, int c);

int main() {
    int num1, num2, num3, max;

    // Asking for user input
    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    // Calling the function to find the maximum number
    max = findMax(num1, num2, num3);

    // Printing the maximum number
    printf("The maximum number is: %d\n", max);

    return 0;
}

// Function to find the maximum of three numbers
int findMax(int a, int b, int c) {
    int max = a;

    if (b > max) {
        max = b;
    }
    if (c > max) {
        max = c;
    }

    return max;
}

Explanation:

  1. Function Prototype:
C
int findMax(int a, int b, int c);

This declares the function findMax which takes three integer arguments and returns an integer.

2. Calling the Function:

C
max = findMax(num1, num2, num3);

3. Printing the Maximum Number:

C
printf("The maximum number is: %d\n", max);

This line prints the maximum number stored in the variable max.

4. Function Definition:

C
int findMax(int a, int b, int c) {
    int max = a;

    if (b > max) {
        max = b;
    }
    if (c > max) {
        max = c;
    }

    return max;
}
  • This defines the findMax function which takes three integers a, b, and c.
  • The function initializes max with the value of a.
  • It then checks if b is greater than max.
  • If true, it updates max to b.
  • It checks if c is greater than max.
  • If true, it updates max to c.
  • Finally, it returns the value of max.

This approach separates the logic of finding the maximum number into a function, making the code more modular and easier to understand.


C Program (Using fmax Function):

C
#include <stdio.h>
#include <math.h>

int main() {
    int num1, num2, num3;
    double max;

    // Asking for user input
    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    // Finding the maximum number using fmax function
    max = fmax(num1, fmax(num2, num3));

    // Printing the maximum number
    printf("The maximum number is: %.0f\n", max);

    return 0;
}

Explanation:

  1. Finding the Maximum Number Using fmax Function:
C
max = fmax(num1, fmax(num2, num3));
  • The fmax function returns the maximum of two numbers.
  • We nest fmax functions to find the maximum of the three numbers. First, we find the maximum of num2 and num3, and then we compare the result with num1 to find the overall maximum

2. Printing the Maximum Number:

C
printf("The maximum number is: %.0f\n", max);

This line prints the maximum number stored in the variable max. The format specifier %.0f is used to print the double value as an integer without any decimal places.

Using the fmax function simplifies the code by leveraging the built-in library function to find the maximum of two numbers and nesting it to handle three numbers.


C Program (Using `qsort` Function):

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

// Comparator function for qsort
int compare(const void *a, const void *b) {
    return (*(int *)b - *(int *)a);  // Sort in descending order
}

int main() {
    int numbers[3];
    
    // Asking for user input
    printf("Enter three numbers: ");
    for (int i = 0; i < 3; i++) {
        scanf("%d", &numbers[i]);
    }

    // Sorting the array in descending order
    qsort(numbers, 3, sizeof(int), compare);

    // The maximum number is the first element after sorting in descending order
    int max = numbers[0];

    // Printing the maximum number
    printf("The maximum number is: %d\n", max);

    return 0;
}
  1. Comparator Function:
C
int compare(const void *a, const void *b) {
    return (*(int *)b - *(int *)a);  // Sort in descending order
}
  • The compare function is used by qsort to determine the order of elements.
  • It compares two integers and returns the difference. Sorting in descending order is achieved by subtracting *a from *b.

2. User Input:

C
printf("Enter three numbers: ");
for (int i = 0; i < 3; i++) {
    scanf("%d", &numbers[i]);
}
  • printf is used to display a message asking the user to enter three numbers.
  • A for loop is used to read the input numbers and store them in the numbers array.

3. Sorting the Array

C
qsort(numbers, 3, sizeof(int), compare);
  • The `qsort` function sorts the numbers array.
  • It takes four arguments: the array to be sorted, the number of elements in the array, the size of each element, and the comparator function.

Using qsort to sort the array in descending order allows us to easily find the maximum number as the first element in the sorted array. This approach is flexible and can handle larger sets of numbers with minimal modification.

©2025 Linux Bangla | Developed & Maintaind by Linux Bangla.