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?

Solutions

Expert Solution

PROGRAM -

import java.util.*;
public class abc
{
public static void main(String[] args)
{
int num;
Scanner s = new Scanner(System.in); //creating scanner object
System.out.println("Enter the number:");
num = s.nextInt(); //taking input from user using scanner object
System.out.println(num);
abc obj = new abc(); //creating object of class abc
int a = obj.fun(num); //calling the function to find no of digits in the given no
System.out.println("NO OF DIGITS IN THE GIVEN NUMBER IS : "+a);
}
int fun(int n)
{
if(n==0)
{
return 0;
}
else
{
return 1+fun(n/10); //recursively calling the function again
}
}
}

OUTPUT -  

COMPLEXITY -

The time complexity of above code is O(no-of-digits in given number).

i.e. if there are k digits in num , the complexity can be written as O(k).


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? use java
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