Project Euler #16: Power digit sum

Question

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?

Answer : 1366

Hacker Rank Problem

Solution:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.*;
import java.math.BigInteger;
public class Solution {

    public static void main(String gg[])
    {
        Scanner scanner=new Scanner(System.in);
        int numberOfTestCases=scanner.nextInt();
        int number=0;
        long sum=0;
        while(numberOfTestCases!=0)
        {
            sum=0;
            number=scanner.nextInt();
            BigInteger bb=new BigInteger("2");
            bb=bb.pow(number);
            String s=bb.toString();
            char c[]=s.toCharArray();
            for(char cc:c)
            {
                sum+=(((int)cc)-48);
            }
            System.out.println(sum);
            --numberOfTestCases;
        }
    }

}

Comments

Popular Posts