Find the Largest Number Using Temporary Variable

In this method, we assume one of the numbers as maximum and assign it to a temporary variable. We then compare this temporary variable with the other two numbers one by one and if the max is smaller than the compared number, we assign the compared number to the max.

C Program to Find the Largest Number Among Three Using Temporary Variable

C




// C Program to Find the Largest Number Among Three using
// Temporary Variable
#include <stdio.h>
int main()
{
    int a = 10, b = 25, c = 3;
 
    // temporary varaible
    int max = a;
 
    if (max < b)
        max = b;
    if (max < c)
        max = c;
 
    printf("Maximum among %d, %d, and %d is: %d", a, b, c,
           max);
 
    return 0;
}


Output

Maximum among 10, 25, and 3 is: 25

Complexity Analysis

  • Time complexity: O(1) as it is doing constant operations
  • Auxiliary space: O(1)

Related Articles



C program to Find the Largest Number Among Three Numbers

Here, we will learn how to find the largest number among the three numbers using the C program. For example, the largest number among 2, 8, and 1 is 8.

Similar Reads

Methods to Find the Largest of Three Numbers

Using if Statement Using if-else Statement Using nested if-else Statement...

1. Find the Largest Number Using if-else Statements

The idea is to use the compound expression where each number is compared with the other two to find out which one is the maximum of them....

2. Find the Largest Number Using Nested if-else Statement

...

3. Find the Largest Number Using Temporary Variable

Unlike the previous method, we consider only one condition in each if statement and put the remaining condition inside it as a nested if-else statement....