Search Here

Apex - integer program

 1. Basic Arithmetic Operations:


You can perform basic arithmetic operations like addition, subtraction, multiplication, and division with integers.

Integer num1 = 10;

Integer num2 = 5;


Integer sum = num1 + num2;

Integer difference = num1 - num2;

Integer product = num1 * num2;

Integer quotient = num1 / num2;


System.debug('Sum: ' + sum);

System.debug('Difference: ' + difference);

System.debug('Product: ' + product);

System.debug('Quotient: ' + quotient);


2. Find the Maximum and Minimum:


You can find the maximum and minimum values from a set of integers using Math.max() and Math.min() functions.


Integer a = 15;

Integer b = 20;


Integer max = Math.max(a, b);

Integer min = Math.min(a, b);


System.debug('Maximum: ' + max);

System.debug('Minimum: ' + min);


3. Check for Even or Odd:


You can check whether an integer is even or odd using the modulo operator (%).


Integer number = 17;


if (number % 2 == 0) {

    System.debug('The number is even.');

} else {

    System.debug('The number is odd.');

}


4. Calculate Factorial:


You can calculate the factorial of an integer using a loop.


Integer num = 5;

Integer factorial = 1;


for (Integer i = 1; i <= num; i++) {

    factorial *= i;

}


System.debug('Factorial of ' + num + ' is ' + factorial);


5. Generate Random Numbers:


You can generate random integers within a specified range using the Math.random() method.


Integer random = (Integer)(Math.random() * 100);

System.debug('Random Number: ' + random);




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.