answersLogoWhite

0


Best Answer

#include<iostream>

#include<list>

class big_num

{

friend std::ostream& operator<< (std::ostream&, const big_num&);

public:

big_num(unsigned __int64 value = 0): m_list()

{

do

{

m_list.push_front ((value % 10) + '0');

} while (value /= 10);

}

big_num& operator*= (unsigned __int64 value)

{

switch (value)

{

case (0):

m_list.clear();

m_list.push_front ('0');

break;

case (1):

break;

default:

unsigned __int64 carry = 0;

for (std::list<char>::reverse_iterator i=m_list.rbegin(); i!=m_list.rend(); ++i)

{

unsigned __int64 digit = (*i - '0') * value + carry;

*i = (digit % 10) + '0';

carry = digit / 10;

}

while (carry)

{

m_list.push_front ((carry % 10) + '0');

carry /= 10;

}

}

return *this;

}

private:

std::list<char> m_list;

};

std::ostream& operator<< (std::ostream& os, const big_num& num)

{

for (std::list<char>::const_iterator i=num.m_list.begin(); i!=num.m_list.end(); ++i)

{

os << *i;

}

return os;

}

int main ()

{

// test big_num for powers of 2 up to and including 2^100

big_num num(1);

unsigned n = 0;

do

{

std::cout << num << std::endl;

num *= 2;

} while(n++<100);

}

Example output:

1

2

4

8

16

32

64

128

256

512

1024

2048

4096

8192

16384

32768

65536

131072

262144

524288

1048576

2097152

4194304

8388608

16777216

33554432

67108864

134217728

268435456

536870912

1073741824

2147483648

4294967296

8589934592

17179869184

34359738368

68719476736

137438953472

274877906944

549755813888

1099511627776

2199023255552

4398046511104

8796093022208

17592186044416

35184372088832

70368744177664

140737488355328

281474976710656

562949953421312

1125899906842624

2251799813685248

4503599627370496

9007199254740992

18014398509481984

36028797018963968

72057594037927936

144115188075855872

288230376151711744

576460752303423488

1152921504606846976

2305843009213693952

4611686018427387904

9223372036854775808

18446744073709551616

36893488147419103232

73786976294838206464

147573952589676412928

295147905179352825856

590295810358705651712

1180591620717411303424

2361183241434822606848

4722366482869645213696

9444732965739290427392

18889465931478580854784

37778931862957161709568

75557863725914323419136

151115727451828646838272

302231454903657293676544

604462909807314587353088

1208925819614629174706176

2417851639229258349412352

4835703278458516698824704

9671406556917033397649408

19342813113834066795298816

38685626227668133590597632

77371252455336267181195264

154742504910672534362390528

309485009821345068724781056

618970019642690137449562112

1237940039285380274899124224

2475880078570760549798248448

4951760157141521099596496896

9903520314283042199192993792

19807040628566084398385987584

39614081257132168796771975168

79228162514264337593543950336

158456325028528675187087900672

316912650057057350374175801344

633825300114114700748351602688

1267650600228229401496703205376

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to write a Program to print powers of 2 in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program in BASIC to find the sum of the series s equals 2 plus 4 plus . plus 100?

10 print "That is not a question." 20 print "That is a command." 30 end


Write a C plus plus function that print a triangle of stars?

Write a function that print a triangle of stars.


C plus plus program to print number patterns?

bghjg


Do I need a C plus plus program to print PASCAL's triangle?

No.


What is the importance of functions in a c plus plus program?

Functions are very important in C++, as you can't write the simplest program to print hello without using a function. Overall you can say that function are building blocks of a C++ program. Functions can also be defined by the programmer to reduce program size.


Write a program to print odd numbers In C plus plus between 51 to 100?

#include &lt;iostream&gt; int main() { for(int i=51; i &lt;= 100; i+=2) { cout &lt;&lt; i &lt;&lt; endl; } return 0; }


Write the steps in c plus plus program to print your name and age at different lines?

#include&lt;iostream&gt; int main() { std::cout&lt;&lt;"your name\nyour age"&lt;&lt;std::endl; return(0); }


How to write a program in c plus plus to print table of even number?

void print_evens (size_t n) {for (size_t x=0; x&lt;=n; x+=2) { std::cout &lt;&lt; x &lt;&lt; std::endl; } }


What are the steps in c plus plus program to print word computer?

std::cout&lt;&lt;"computer"&lt;&lt;std::endl;


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


Can you write a program to read a set of scores from a file and compute the average and print it on the screenby c plus plus?

Yes. Use cin and/or getline to read the formatted data into an array, compute the average then output the result using cout.