//Hi,i need to create programm,which takes the user input,determins how many digits in integer
// and than raise this digit to power,what was entered by user too.
//first function noraml,second one to rais to power recursive. So if user enter number
//234,this is 3 digits,so now need recurcively 3 raise to the power .
// This  what i got so far program determins the digits from integer,but does not use power


// Power of size number.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<string>
using namespace std;
typedef long double ld;

int countDigits(int number) {
    if (number < 10) {
        return 1;
    }
    int count = 0;
    while (number > 0) {
        number /= 10;
        count++;
    }
    return count;
}

int place(ld,ld);
// testing the countDigits function
int main() {
    int num ;
    int result;
    int exp;
    cout << "enter number: "<<endl;
    cin >> num;
    cout << "Enter exponent: " << endl;
    cin >> exp;
    int numDigits = countDigits(num);
    cout << num << " has " << numDigits << " digits" << endl;
    result = place(numDigits,exp);
    cout << result;
    return 0;
}int place(ld numDigits,ld exp)
{
    if(exp >=10)
    {
     return
         numDigits*(place(numDigits,exp-1));


    }
    else
        return 1;



}

Recommended Answers

All 8 Replies

Your condition at line 45 if(exp >= 10) is wrong. Remeber that any number raise to the power of 0 is 1 but this is not what is embodied by your condition.

I would also say you are missing a newline or 2 at line 43.

Also storing exp as a long double in the function place could lead to error because all floating point types are only approximations so 10.0 minus 1.0 10 times may not equal exactly 0.0; however since you originally request the exponent as an integer, line 37, you can safely hold it as an integer for the whole calculation avoiding the floating point issue altogether.

A couple of other things:

  • They way you've interpreted the question means that your input number can't be more than 63, since a 64-bit integer(the largest most modern systems will recognize) will max out after that. It makes more sense to raise the input number to the power of the number of digits(i.e. 234 raised to the power of 3)

  • The way you've approached the algorithm is flawed. It is doubling the result each time, instead of multiplying it by the base number.

Here is an example you can look at:

unsigned long long powers(int base, int count, unsigned long long pow)
{
    if (count == 0)
    {
        return pow;
    }
    return powers(base, --count, pow * base);
}
How would i express the code  for example, place(412) = 1000,so power is always 10 and number of digits in that integer 3,so 3^10 and all recursively.
Is were any way to do all in one function?And just ask user for input base.

The code I gave you will work, just pass the 10 as the base and use the result of the digit count function as the count and set pow to 1 or set pow as an optional parameter with a default value of 1:

unsigned long long powers(int base, int count, unsigned long long pow = 1)
{
    if (count == 0)
    {
        return pow;
    }
    return powers(base, --count, pow * base);
}

int countDigits(int number) {
    if (number < 10) {
        return 1;
    }
    int count = 0;
    while (number > 0) {
        number /= 10;
        count++;
    }
    return count;
}

int main() 
{
    int num;
    unsigned long long result;
    cout << "enter number: " << endl;
    cin >> num;
    result = powers(10, countDigits(num));
    cout << result;
    return 0;
}    

.

Thank you

Glad I could help. Please remember to mark this solved. Thanks.

The way you've approached the algorithm is flawed. It is doubling the result each time, instead of multiplying it by the base number.

That's not quite true, the line doing the calculation is

 return numDigits*(place(numDigits,exp-1));

That's not doubling that is multiplying by the base (the base in this case being named numDigits).

I agree that raising number of digits to power rather than base to number of digits is strange but it is what the question says is required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.