function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prajwal B 1Prajwal B 1 

code to compare 3 numbers and find out largest and smallest number

Abdul KhatriAbdul Khatri
Hi Prajwal,

You can simply use the List in apex to get that, here is the sample code
 
List<Integer> numList = new List<Integer>{0,56,7,2,34};
numList.sort();
system.debug('smallest : ' + numList.get(0));
system.debug('larget : ' + numList.get(numList.size()-1));

I hope this will help.

Please don't forget to mark it a best answer
sanket mahajan 1sanket mahajan 1
Hi Prajwal,
Can you explain the busniess scenario if the above answer is not useful?
Ameria ZoneAmeria Zone
Is this satisfy your query, if not you can still ask for it. (http://apkeast.com)
Prajwal B 1Prajwal B 1
Hi,
can the code be written using loops?
H 007H 007
public class CompareNumbers {
    public static void compare(int num1, int num2, int num3) {
        int largest = num1;
        int smallest = num1;
        int[] nums = new int[]{num1, num2, num3};
        for (int i = 0; i < 3; i++) {
            if (nums[i] > largest) {
                largest = nums[i];
            }
            if (nums[i] < smallest) {
                smallest = nums[i];
            }
        }
        System.debug('Largest number is: ' + largest);
        System.debug('Smallest number is: ' + smallest);
    }
}

// Example usage:
CompareNumbers.compare(10, 5, 8); // Output: Largest number is: 10, Smallest number is: 5