Calculating the sum of the digits in 2^1000 I have a program which calculates the sum of the digits in the number 21000. The program is not beautiful and I was hoping for some advice on how to improve it.

The idea I have used is to create an array with the first element equal to 20. The first element in the array represent 100 the second 101 and so on.
Then go through and double every element in the array. After every element is doubled I check if an element in the array is greater than 9, if it is I subtract 10 and add a 1 to the next element in the array. For example if the element at place 40 is equal to 13 and element at place 41 is equal to 3 then I will change the element at place 40 to 3 and element at place 41 to 4.

#include &lt;stdio.h&gt;
int main(void){
 int sum = 0;
 int dec[320] = {1, };
 int i,j,p,q;
 for(i = 1; i &lt; 1001; i++){

   for (j = 0; j &lt; 302; j++) {
     dec[j] = 2*dec[j];
     }

     for(q = 0; q &lt; 302; q++){
       if(dec[q] &gt; 9){
         dec[q] = dec[q] % 10;
         dec[q+1] = dec[q+1] + 1;
     }
   }

 }

 for(p = 0; p &lt; 302; p++){
   sum += dec[p];
 }
 printf("sum = %d sista %d \n", sum, dec[301]);
}
I see some things that may help you improve your code.

Eliminate "magic numbers"

The code, is full of "magic numbers" -- that is, raw numbers in the code that don't have obvious meaning.  For example, we have this:

int dec[320] = {1, };
// and later ...
for (j = 0; j &lt; 302; j++) {


It's unclear, whether 302 and 320 is just a typo or has some signficance.  Better would be to use a #define for this.  I'd use this:

#define BITS 1000
#define DIGITS 302


Write in idiomatic C

Lines like these are a little peculiar to an experienced C programmer:

dec[q] = dec[q] % 10;
dec[q + 1] = dec[q + 1] + 1;


The idiom instead would be to write them like this:

dec[q] %= 10;
++dec[q + 1];


Avoid potentially expensive operations

Since you're only doubling each digit each pass, it's certain that the maximum possible value is 9*2 = 18, so rather than using the % operator, it's sufficient to simply subtract 10.

Make only a single pass through

There's no need to make two passes through the array for each doubling.  It can be done in a single pass like this:

for (int i = BITS; i; --i) {
    int carry = 0;
    for (int j = 0; j &lt; DIGITS; j++) {
        dec[j] = 2 * dec[j] + carry;
        if (dec[j] &gt; 9) {
            dec[j] -= 10;
            carry = 1;
        } else {
            carry = 0;
        }
    }