Friday, November 15, 2013

Sum of digits


As an example, the sum of the digits of 2 to the 10th power is:

2^10 = 1024  => 1 + 0 + 2 + 4 => 7


What is the sum of the digits of  2^50?
Note:  ^  means exponent

#include<iostream>
using namespace std;

class sumofdigit {
 
             private:
                    long long int val;
             public:
                    sumofdigit() {
                              val = 1;
                    }
                    void calc_sumofdigit () {
                              long long int temp = 0;
                              int i,sum = 0;
                              for (i = 1 ; i <= 50 ; i++) { 
                                          val = val * 2;
                              }
                              cout<<"2^50 = "<<val<<endl;
                              while (val != 0) { 
                                          temp = val % 10;
                                          sum = sum + temp;
                                          val = val/10;
                               }
                               cout<<"Sum = "<<sum<<endl;
                     }
};

int main() { 
        sumofdigit obj;
        obj.calc_sumofdigit();
        return(0);
}

Output:


2^50 = 1125899906842624
Sum = 76

By +Dixit Singla 

No comments: