DEV Community

vimala jeyakumar
vimala jeyakumar

Posted on

Count of digits and Palindrome

Count of Digits:

-->Given a number n, the task is to return the count of digits in this number.
Example:

Input: n = 1567
Output: 4
Explanation: There are 4 digits in 1567, which are 1, 5, 6 and 7
Enter fullscreen mode Exit fullscreen mode

-->The idea is to count the digits by removing the digits from the input number starting from right(least significant digit) to left(most significant digit) till the number is reduced to 0.
--> We are removing digits from the right because the rightmost digit can be removed simply by performing integer division by 10. For eg: n = 1567, then 1567 / 10 = 156.7 = 156(Integer Division).

Flowchart:

Image description

Example:

package programs;

public class CountOfDigit {

    public static void main(String[] args) 
    {
        int no = 54321;
        int count = 0;
        while(no>0)
        {
            //System.out.println(no%10);
            //no = no/10;
            count = count+1;

        }
        System.out.println("count: "+count);

    }
}

Enter fullscreen mode Exit fullscreen mode

Output:
1
2
3
4
5
count: 5

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay