Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,849 questions

55,678 answers

573 users

How to sum the digits of the number 2^N in Java

2 Answers

0 votes
public class PowerOfTwoDigitSum {

    public static String calculate2PowerNAsString(int N) {
        // Calculate 2^N as a string for large numbers
        StringBuilder result = new StringBuilder("1");

        for (int i = 0; i < N; i++) {
            int carry = 0;
            for (int j = 0; j < result.length(); j++) {
                int digit = result.charAt(j) - '0';
                int num = digit * 2 + carry;
                result.setCharAt(j, (char) ((num % 10) + '0'));
                carry = num / 10;
            }
            while (carry > 0) {
                result.append((char) ((carry % 10) + '0'));
                carry /= 10;
            }
        }

        return result.toString();
    }

    public static int sumOfDigits(int N) {
        String result = calculate2PowerNAsString(N);
        
        int sum = 0;
        for (int i = 0; i < result.length(); i++) {
            sum += result.charAt(i) - '0';
        }
        
        return sum;
    }

    public static void main(String[] args) {
        int N = 15;
        System.out.println("Sum of digits of 2^" + N + " is: " + sumOfDigits(N));

        N = 100;
        System.out.println("Sum of digits of 2^" + N + " is: " + sumOfDigits(N));

        N = 1000;
        System.out.println("Sum of digits of 2^" + N + " is: " + sumOfDigits(N));
    }
}



/*
run:

Sum of digits of 2^15 is: 26
Sum of digits of 2^100 is: 115
Sum of digits of 2^1000 is: 1366

*/

 



answered Aug 1, 2025 by avibootz
0 votes
import java.math.BigInteger;

public class PowerOfTwoDigitSum {

    public static int sumOfDigits(int N) {
        BigInteger power = BigInteger.valueOf(2).pow(N); // Compute 2^N
        String digits = power.toString(); // Convert to string
        
        int sum = 0;
        for (char digit : digits.toCharArray()) {
            sum += Character.getNumericValue(digit); // Sum each digit
        }
        
        return sum;
    }

    public static void main(String[] args) {
        int N = 15;
        System.out.println("Sum of digits of 2^" + N + " is: " + sumOfDigits(N));

        N = 100;
        System.out.println("Sum of digits of 2^" + N + " is: " + sumOfDigits(N));

        N = 1000;
        System.out.println("Sum of digits of 2^" + N + " is: " + sumOfDigits(N));
    }
}



/*
run:

Sum of digits of 2^15 is: 26
Sum of digits of 2^100 is: 115
Sum of digits of 2^1000 is: 1366

*/



 



answered Aug 1, 2025 by avibootz

Related questions

...