Average of Digits in an Integer

less than 1 minute read

The code has been modified slightly and now uploaded to the site.

Question

<— Return Home

62.cpp

#include <iostream>
using namespace std;

double averageDigits(long n){
    double add = 0;
    // for (int i = 0; i < 20; i++){
    while (n != 0){
        add += n % 10;
        n /= 10;
    }
    return add;
}

int main(){
    long value = 235323522;
    double result = averageDigits(value);
    std::cout << result << "\n";
}

<— Return Home