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
You can simply use the List in apex to get that, here is the sample code
I hope this will help.
Please don't forget to mark it a best answer
Can you explain the busniess scenario if the above answer is not useful?
can the code be written using loops?
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