0

I have made a C program of 2^0 + 2^1 + 2^2 + 2^3 +......... 2^64. My problem is that such a big value is not being stored. Which data type should I use? Here is my code:

#include<stdio.h>
#include<conio.h>
void main()
{
   int i;
   int number=2;
   int total=1;
   int power=64;
   int totalSum=1;
   for(i=1;i<=power;)
   {
      total=total*number;
      totalSum=totalSum+total;
      i++;
   }

   printf("%d\n",total);
   printf("Sum=%d",totalSum);
}

Kindly can you please tell me that which type of data type should I use i C language

LPs
  • 16,045
  • 8
  • 30
  • 61
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
  • 2
    Please refere to this [link in stack](http://stackoverflow.com/questions/2252896/how-to-store-a-very-long-integer-value-in-a-c-program-for-an-exam-98474737475) – makdu Oct 07 '15 at 03:46
  • 2
    Look for a bigint implementation. Here's a C example. http://codereview.stackexchange.com/questions/20783/a-small-biginteger-implementation-i-wrote-in-c-my-first-decent-c-program-plea – Bill Ravdin Oct 07 '15 at 03:47
  • 2
    uint128_t is not enough, 10^40 is a bit greater than 2^132, so at least 133 bits are needed. – rcgldr Oct 07 '15 at 04:20
  • 2
    A few comments on the posted code: 1) the return type from main() is always 'int', never 'void'. 2) For readability and ease of understanding, please consistently indent the code. 3) the 'i++' should be the third parameter of the 'for()' statement 4) you probably should read about the operators: '+=' and '*=' 5) the variables 'total' and 'totalsum' will have to be from the bignum library, 'int' is way too small – user3629249 Oct 07 '15 at 05:36
  • Implement your own arithmetic functions for big integers. Store the digits in a char array of std::string. – anurag-jain Oct 07 '15 at 08:13

0 Answers0