C program to find maximum between two numbers

List Topics
June 23, 2024
No Comments
6 min read

Below is a simple C program that finds the maximum between two numbers:

C
#include <stdio.h>

int main() {
    int num1, num2;

    // Input two numbers from user
    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

    // Check which number is greater
    if (num1 > num2) {
        printf("The maximum number is: %d\n", num1);
    } else if (num2 > num1) {
        printf("The maximum number is: %d\n", num2);
    } else {
        printf("Both numbers are equal.\n");
    }

    return 0;
}

Explanation:

1. Include necessary header files:

C
#include <stdio.h>

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

2. Declare main function:

C
int main() {

3. Declare variables to store the two numbers:

C
int num1, num2;

4. Prompt the user for input and read the numbers:

C
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

5. Compare the two numbers and print the maximum:

C
if (num1 > num2) {
    printf("The maximum number is: %d\n", num1);
} else if (num2 > num1) {
    printf("The maximum number is: %d\n", num2);
} else {
    printf("Both numbers are equal.\n");
}

6. Return 0 to indicate successful completion of the program:

C
return 0;

Have another way Use `Ternary Operator`

* Find the maximum between two numbers in C using the ternary operator.

C
#include <stdio.h>

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

    // Input two numbers from user
    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

    // Use ternary operator to find the maximum number
    max = (num1 > num2) ? num1 : num2;

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

    return 0;
}

Use the ternary operator to find the maximum number:

C
max = (num1 > num2) ? num1 : num2;

The ternary operator ? : is a shorthand for the if-else statement. It evaluates the expression before the ?. If it's true, it returns the value before the :; otherwise, it returns the value after the :.


Have another way Use `Functions`

*Find the maximum between two numbers in C by using a function.

C
#include <stdio.h>

// Function declaration
int findMax(int a, int b);

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

    // Input two numbers from user
    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

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

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

    return 0;
}

// Function to find the maximum between two numbers
int findMax(int a, int b) {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}

1. Function declaration:

C
int findMax(int a, int b);

This declares a function `findMax` that takes two integers as parameters and returns an integer.

2. Call the function to find the maximum number:

C
max = findMax(num1, num2);

3. Define the function to find the maximum between two numbers:

C
int findMax(int a, int b) {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}

In this approach, the `findMax` function encapsulates the logic to determine the maximum number, making the code more modular and reusable. Compile and run this program in a C compiler to see it in action.


Have another way Use `Standard Library Function`

* Find the maximum between two numbers in C by using the standard library function `fmax`

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

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

    // Input two numbers from user
    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter second number: ");
    scanf("%lf", &num2);

    // Use fmax function to find the maximum number
    max = fmax(num1, num2);

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

    return 0;
}

Explanation:

1. Include necessary header files:

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

The math.h library is included to use the fmax function.

2. Declare variables to store the two numbers and the maximum:

C
double num1, num2, max;

3. Prompt the user for input and read the numbers:

C
printf("Enter first number: ");
scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);




4. Use the fmax function to find the maximum number:

C
max = fmax(num1, num2);

5. Print the maximum number:

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

Note:

  • fmax is a function in the math library that returns the maximum of two floating-point numbers. It handles various edge cases and is optimized for performance.
  • Since fmax deals with floating-point numbers, the program uses double type for the variables and scanf with %lf to read the input.

Compile and run this program in a C compiler to see it in action.


Have another way Use `Standard Library Function`

* Find the maximum between two numbers in C using `pointers`

C
#include <stdio.h>

// Function declaration
int findMax(int* a, int* b);

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

    // Input two numbers from user
    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

    // Call the function to find the maximum number using pointers
    max = findMax(&num1, &num2);

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

    return 0;
}

// Function to find the maximum between two numbers using pointers
int findMax(int* a, int* b) {
    if (*a > *b) {
        return *a;
    } else {
        return *b;
    }
}

Short Explanation:

1. Function declaration:

C
int findMax(int* a, int* b);

2. Call the function to find the maximum number using pointers:

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

3. Define the function to find the maximum between two numbers using pointers:

C
int findMax(int* a, int* b) {
    if (*a > *b) {
        return *a;
    } else {
        return *b;
    }
}

How It Works:

  • Pointers: The findMax function takes two pointers to integers. It compares the values at the addresses pointed to by the pointers.
  • Dereferencing: The *a and *b expressions are used to access the values pointed to by a and b, respectively.
  • Comparison: The function compares the values and returns the greater one.

Compile and run this program in a C compiler to see it in action. This approach demonstrates the use of pointers and can be useful in various scenarios involving memory management.

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