mahnoor fatima
mahnoor fatima

Reputation: 41

Counting number of digits in an integer through recursion

My code is following:

/counting number of digits in an integer

#include <iostream>
using namespace std;

int countNum(int n,int d){
    if(n==0)
    return d;
    else
    return (n/10,d++);
}
int main(){
    int n;
    int d;
    cout<<"Enter number"<<endl;
    cin>>n;
   int x=countNum();
    cout<<x;
    return 0;
}

i cannot figure out the error,it says that : too few arguments to function `int countNum(int, int)' what is issue?

Upvotes: 0

Views: 10069

Answers (9)

Abdullah Dilshad
Abdullah Dilshad

Reputation: 1

Inform yourself about the PMI (Principle of Mathematical Induction) on YouTube. It makes recursion very easy. Try this:

#include<iostream>
using namespace std;
    
int count(int n){
    //Base Case
    if(n == 0){
        return 0;
    }
    // Recursive Step
    // We don't have to worry about the smaller steps.
    // Don't go deep into the recursion!
    int smallerInput  = count(n/10);
    
    //Calculation Step
    return smallerInput + 1;
}   

int main()
{   
    int num;
    cout << "Enter the number: ";
    cin >> num;
    
    cout << "The number of digits are : " << count(num) << endl;
    return 0;
}

Upvotes: 0

Sergii Khaperskov
Sergii Khaperskov

Reputation: 441

#include <iostream>
using namespace std;

int countNum(int n,int d){
    if(n<10)
        return d;
    else
        return countNum(n/10, d+1);
}
int main(){
    int n;
    cout<<"Enter number"<<endl;
    cin>>n;
    int x=countNum(n, 1);
    cout<<x;
    return 0;
}

Upvotes: 0

Sean
Sean

Reputation: 62472

Change it to:

int x=countNum(n,0);

You don't need to pass d in, you can just pass 0 as the seed.

Also change countNum to this:

int countNum(int n,int d){
    if(n==0)
    return d;
    else
    return coutNum(n/10,d+1); // NOTE: This is the recursive bit!
}

Upvotes: 0

Zac Howland
Zac Howland

Reputation: 15872

Assuming this is not for an assignment, there are better ways to do this (just a couple of examples):

Convert to string

unsigned int count_digits(unsigned int n)
{
    std::string sValue = std::to_string(n);
    return sValue.length();
}

Loop

unsigned int count_digits(unsigned int n)
{
    unsigned int cnt = 1;
    if (n > 0)
    {
        for (n = n/10; n > 0; n /= 10, ++cnt);
    }
    return cnt;
}

Tail End Recursion

unsigned int count_digits(unsigned int n, unsigned int cnt = 1)
{
    if (n < 10)
        return cnt;
    else
        return count_digits(n / 10, cnt + 1);
}

Note: With tail-end recursion optimizations turned on, your compiler will transform this into a loop for you - preventing the unnecessary flooding of the call stack.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

Your function is written incorrectly. For example it is not clear why it has two parameters or where it calls recursively itself.

I would write it the following way

int countNum( int n )
{
   return 1 + ( ( n /= 10 ) ? countNum( n ) : 0 );
}

Or even it would be better to define it as

constexpr int countNum( int n )
{
   return 1 + ( ( n / 10 ) ? countNum( n/10 ) : 0 );
}

Upvotes: -2

Shoe
Shoe

Reputation: 76240

Because you declared the function to take two arguments:

int countNum(int n,int d){

and you are passing none in:

int x = countNum();

You probably meant to call it like this, instead:

int x = countNum(n, d);

Also this:

return (n/10,d++);

should probably be this:

return countNum(n/10,d++);

Also you are not initializing your n and d variables:

int n;
int d;

Finally you don't need the d argument at all. Here's a better version:

int countNum(int n){
    return (n >= 10)
        ? 1 + countNum(n/10)
        : 1;
}

and here's the working example.

Upvotes: 6

Alex Telishev
Alex Telishev

Reputation: 2274

Because you haven't passed parameters to the countNum function. Use it like int x=countNum(n,d);

Upvotes: 0

m24p
m24p

Reputation: 674

Your code here:

int x=countNum();

countNum needs to be called with two integers. eg

int x=countNum(n, d);

Upvotes: 0

Sunil Bojanapally
Sunil Bojanapally

Reputation: 12658

int x=countNum(); the caller function should pass actual arguments to calling function. You have defined function countNum(int, int) which means it will receive two ints as arguments from the calling function, so the caller should pass them which are missing in your case. Thats the reason of error too few arguments.

Upvotes: 2

Related Questions