Code Logo

Find Larger Number

Published at19 Apr 2026
Searching & Sorting Easy 8 views
Like0

Given two integers, return the larger of the two. If they are equal, return either one (they are the same). This is a simple comparison problem that teaches conditional logic.

For example, the larger of 5 and 3 is 5. The larger of -2 and 7 is 7. The larger of 10 and 10 is 10 (either is correct).

Finding the larger of two numbers is the simplest form of comparison-based decision making. It teaches the if-else conditional structure and the greater-than operator. This pattern is the building block for more complex operations like finding the maximum of an array, sorting algorithms, and tournament-style elimination.

The solution compares a and b using if-else logic. If a is greater than b, return a. Otherwise, return b. This handles all cases including equal numbers.

Edge cases include equal numbers (return either), negative numbers (comparison works naturally), and very large numbers where the comparison still works correctly.

Learn about our pseudocode specification
Guide

Example Input & Output

Example 1
Input
a = 7, b = 7
Output
7
Explanation

Both values are equal, so the result is 7.

Example 2
Input
a = 3, b = 12
Output
12
Explanation

12 is larger than 3.

Example 3
Input
a = 9, b = 4
Output
9
Explanation

9 is larger than 4.

Algorithm Flow

Recommendation Algorithm Flow for Find Larger Number
Recommendation Algorithm Flow for Find Larger Number

Solution Approach

Compare the two numbers using a conditional statement and return whichever is larger.

function larger(a, b)
  if a > b then return a
  else return b

If a is greater than b, return a as the larger value. Otherwise, return b. This works correctly when a equals b — the else branch returns b, which has the same value as a. The greater-than comparison operator works correctly for positive numbers, negative numbers, and zero without any special handling.

Time complexity is O(1) since only a single comparison is performed. Space complexity is O(1).

Best Answers

Pseudocode - Approach 1
program find_larger_number
dictionary
   a, b: integer
algorithm
   input(a, b)
   if a >= b then
      output(a)
   else
      output(b)
   endif
endprogram