Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

counting digts in c++ using log10

#include <iostream>
#include <math.h>

using namespace std;

int main() {
    int n, temp, rem, digits=0, sum=0;
    cout << "Enter a armstrong number: ";
    cin >> n;
    temp = n;
    digits = (int)log10(n) + 1;

    while (n != 0) {
        rem = n % 10;
        sum = sum + pow(rem, digits);
        n = n / 10;
    }

    if (temp == sum) {
        cout << "yes";
    }
    else {
        cout << "not";
    }
}

How does the " digits = (int)log10(n) + 1; " line actually calculates the digits?
can anyone explain?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Math.

Logarithms are basically "exponents in reverse." Log10(100) is 2.0, as 10 to the second power is 100.

Cast to 'int and add one to that are you get 3, which is the number of digits.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading