Question

1. We can determine how many digits a positive integer has by repeatedly dividing by 10 (without keeping the remainder) until the number is less than 10, consisting of only 1 digit. We add 1 to this value for each time we divided by 10. a) Describe the algorithm in a recursive way b) Implement this recursive algorithm in C++ and test it using a main function that calls this with the values of 15, 105 and 15105. (Hint: Remember if n is an integer, n/10 will be an integer without the fractional part.)
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>
#include<string>

using namespace std;

int count_digit(int num){
   if(num == 0){
      return 0;
   }
   else{
      return 1 + count_digit(num/10);
   }
}

int main() {
   cout<<"count_digit(15) = "<<count_digit(15)<<endl;
   cout<<"count_digit(105) = "<<count_digit(105)<<endl;
   cout<<"count_digit(15105) = "<<count_digit(15105)<<endl;
   system("pause");
   return 0;
}

count digit152 count_digit(1053 count digit 15105 5 Press any key to continue . - . Process exited after 1.302 seconds with returnvalue 0 Press any key to continue - -

\color{blue}Please\; up\;vote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
1. We can determine how many digits a positive integer has by repeatedly dividing by 10...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implementing a recursive function that calculates the remainder of dividing a very large integer by three...

    Implementing a recursive function that calculates the remainder of dividing a very large integer by three Implementing a recursive function that calculates the remainder of dividing a very large integer by three 1 .) Remainder of Dividing by Three The easiest way to find the remainder of a given integer like 235 when dividing by three is to calculate the remainder of 2 + 3 + 5 instead. Use this idea to implement a recursive function that calculates the remainder...

  • In this assignment, you are asked to implement a Java class named HugeInt for storing huge...

    In this assignment, you are asked to implement a Java class named HugeInt for storing huge integers and performing mathematical operations on them. This class must have a private variable of type LinkedList〈Byte〉 which stores digits of a huge integer and ten of the following public methods (choose ten methods out of 12): public void setValue (int value): gets an integer (like 1234) as input parameter and stores its digits in the linked list in the same order as they...

  • 1. (Sum the digits in an integer) Write a method that computes the sum of the...

    1. (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n) For example, sumDigits (234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10(= 4). To remove 4 from 234, use 234 / 10(= 23)....

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • IN PYTHON! Radix Conversion(*UP TO BASE 37*) Constraints: • Use only basic arithmetic and comparison operations....

    IN PYTHON! Radix Conversion(*UP TO BASE 37*) Constraints: • Use only basic arithmetic and comparison operations. Do not make any function calls other than the recursive call to convertBase Write a recursive function: convertBase(n,base) that will convert the non-negative decimal number n (type integer) to the specified base and return it as a string. Note that decimal values can be converted to a different number base by repeatedly dividing the residual quotient of n // base until it is zero,...

  • Exercise 6: How many positive integers between 1 and 10", with n 1 a positive integer,...

    Exercise 6: How many positive integers between 1 and 10", with n 1 a positive integer, does not have 7 as a digit in their base 10 representation?

  • Write a Python script named assignment2.py that implements the following steps given an integer value: 1.)...

    Write a Python script named assignment2.py that implements the following steps given an integer value: 1.) Double the value of every second digit beginning from the right. For example, the number 1386 has digits [1, 3, 8, 6] which become [2, 3, 16, 6]. 2.) Add the digits of the doubled values and the digits that were not doubled from the original number. For example, [2, 3, 16, 6] becomes 2 + 3 + 1 + 6 + 6 =...

  • Using c++ A ten diglt ISBN number uses a checksum as its last diglt to verlfy...

    Using c++ A ten diglt ISBN number uses a checksum as its last diglt to verlfy the first nine digits are valid. Before 2007, all ISBN numbers were composed like this, such as: e-20-5e8005-7 or 1-234-56789-X The first nine digits are assigned by a book's publisher and the last digit is calculated by "weighted sum (described below). The X stands for the checksum value of 10, in order to represent ten as a single digit. You must write a program...

  • Pseudo-random numbers are pervasive and extremely important in modern computing and scientific applications. But how exactly...

    Pseudo-random numbers are pervasive and extremely important in modern computing and scientific applications. But how exactly is a sequence of apparently random number generated? Here we study one early method which has the benefit of being very easy to implement 1. If we take a positive integer n having k digits (k 1), then n 10*, so that n2 (10)2 02. Thus we would expt up to 2k digits in the square of the k digit number 1l So, for...

  • IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we...

    IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we do the following: If the integer is odd, we multiply by 3 and add 1. If the integer is even, we divide by 2. It is hypothesized that the above sequence will always converge to the value of 1, regardless of any valid initial choice. This hypothesis is known as the Collatz Conjecture. For example, if we start at 5, the numbers generated by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT