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.
![[Solved] Number of digits with Java [Solved] Number of digits with Java](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-Number-of-digits-with-Java.png)
![[Solved] allExponent write your code here for negative exponentInput](https://realcoder.techss24.com/wp-content/uploads/2022/11/Solved-allExponent-write-your-code-here-for-negative-exponentInput-300x199.png)
![[Solved] Help Lost Child Display with Java](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-Help-Lost-Child-Display-with-Java-300x200.png)
![[Solved] New Administrative Policy with Java, C++, Python](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-New-Administrative-Policy-with-Java-C-Python-300x200.png)