Question

In: Computer Science

Given a positive integer n, write a recursive algorithm that returns the number of the digits...

Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm? use java

Solutions

Expert Solution

1. Take input from user and store it in a variable "num"
2. Check whether value of num is valid i.e integer and positive
3. if value is not valid repeat step 1 and 2 until user enter valid value
4. if value is valid call recursive function with parameter as "num" do step 5 and 6
5. check if value is 0, if 0 return 0
6. else increase counter by 1 and go to step 4 and make num=num/10

import java.io.*; 
import java.util.Scanner;

class Main 
{ 
    public static int count=0;// to count number of digits
    // method to calculate digits in a number
    // It will check whether n=0 or not
    // if n=0 it will terminate
    // else if will call itself with n/10 and count will be increased by 1
    static int digits(int n) 
    {  
        if (n == 0) 
            return 0; 
        count++;
        return (digits(n / 10)); 
    } 
    // main function
    public static void main(String args[]) 
    { 
        int num=0;// to store user entered value
        boolean invalid=true; // to check whether value entered by user is valid or not
        do
        {
            try
            {
                Scanner sc = new Scanner(System.in);
                // range of int is from 2,147,483,648 to 2,147,483,647 i.e upto 10 digits
                // if we try to enter more than 10 digits it will throw an exception 
                // we can change this limit by using other data types in place of int
                System.out.println("Enter positive integer upto 10 digits");
                num=sc.nextInt();
                // if num is positive it will be accepted else user will be prompted to enter number once again
                if(num>=0) 
                {
                    invalid=false;
                }
                else 
                {
                    invalid=true;
                }
            }
            // exception will be thrown if user tries to enter any value other than integer
            // i.e string value , char value, value greater than range of integer etc.
            catch(Exception e)
            {
                invalid=true;
            }
        }while(invalid);
        // call to digit function with user entered value
        if(num==0)
        {
            System.out.println("digits in " +  num + " is 1");   
        }
        else
        {
            int result = digits(num); 
            System.out.println("digits in " +  num + " is " + count); 
        }
        
    } 
}

If we consider digits in number =d
then our complexity becomes O(d) because function will be called d+1 times that will evaluated to d
therefore complexity=O(d)


Related Solutions

Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm?
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
that, given an integer N and an integer K, returns the minimum number of rounds that...
that, given an integer N and an integer K, returns the minimum number of rounds that are necessary for John to leave the casino with N chips, having played all-in no more than K times.
Given a stack S and an element x, write a recursive algorithm that returns True if...
Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Given a stack S and an element x, write a recursive algorithm that returns True if...
Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Develop, write and certify a properly recursive procedure reverseDigits to input a positive integer n and...
Develop, write and certify a properly recursive procedure reverseDigits to input a positive integer n and to output the integer formed by reversing the digits of n. Thus (reverseDigits 1234) returns the integer 4321. Please answer using Dr Racket(R5RS language)
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
Envision an algorithm that when given any positive integer n, it will print out the sum...
Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. E.g. given 4 the algorithm would print 30 (because 1 + 4 + 9 + 16 = 30) You can use multiplication denoted as * in your solution and you do not have to define it (e.g. 2*2=4) Write pseudocode for this algorithm using iteration (looping). Create a flow chart Implement solution from flowchart in Python at...
Use java .Given a stack S and an element x, write a recursive algorithm that returns...
Use java .Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT