[Solved] Number of digits with Java

Write a program to find the number of digits in a number using recursion.

Input and Output Format:

Input consists of a nonnegative integer.

Refer sample input and output for formatting specifications.

All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output:

Enter the value of n

432

The number of digits in 432 is 3

Solution

import java.util.*;

public class Main {

    public static void main(String[] args)

    {

        int n;

        System.out.println("Enter the value of n");

        Scanner s = new Scanner(System.in);

        n = s.nextInt();

        System.out.println("The number of digits in " + n + " is " + (findnum(n / 10)));

    }

    public static int findnum(int n) {

        if (n == 0)

        {

            return 1;
        } else {

            return (+findnum(n / 10));

        }

    }

}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *