top of page

Write a program in salesforce to count the number of digit in a integer using a loop.

  • Writer: Abhilash Banpurkar
    Abhilash Banpurkar
  • Jan 21, 2023
  • 1 min read

Updated: Jan 23, 2023

Here is an example of a Salesforce Apex class that counts the number of digits in an integer using a loop:


Program

public class CountDigits {
    public static Integer count(Integer num) {
        Integer count = 0;
        while (num != 0) {
            num /= 10;
            count++;
        }
        return count;
    }
}

You can call this class from your Apex code or from a Visualforce page to count the number of digits in an integer. For example, if you want to count the number of digits in the integer 12345, you can call the following:


Execution :


Integer result = CountDigits.count(12345); System.debug(result);



Output:

This will give you the number of digits in the integer 12345 which is 5.

 
 
 

Recent Posts

See All

Comments


bottom of page