算法9:What is the sum of the digits of the number 21000

 

 

 What is the sum of the digits of the number 21000

 

 

    public static int sumDigits(int n) {
        int int_retVal = 0;
        int int_pow = (int) Math.pow(2, n);

        String str_retVal = String.valueOf(int_pow);
        for (int i = 0; i < str_retVal.length(); i++) {
            int_retVal += Integer.parseInt(String.valueOf(str_retVal.charAt(i)));
        }
        System.out.println(int_pow + ",  " + int_retVal);
        return int_retVal;
    }


    public static void main(String[] args) {

        sumDigits(1000);
    }

结果

2147483647,  46

 

posted @ 2020-06-24 17:23  BORS  阅读(247)  评论(0编辑  收藏  举报
bors