Thread: my nifty little exponent program

  1. #1

    my nifty little exponent program

    hello all. I'm a beginning programmer and I thought that I would try to write a program that would take a digit and raise it to a large number. It works pretty well and all, but it starts returning all zeros for number larger than 4^15. I would appreciate it if anyone could tell me why? Thank you

    int add=0;
    int mypow=0;
    int diff=0;
    int base=0;
    int carry=0;
    int lnumber[99]={0};


    void remain(int fnum, int fnum2)
    for (int k=0; k<100; k+=10)
    if (fnum*fnum2>=k)


    void mult(int farr[], int foper)
    for (int i=99; i>0; i--)
    remain(farr[i], foper);

    void power(int farr2[],int fexp,int fbase)
    for (int i=1;i<fexp;i++)

    #include <iostream>
    using namespace std;

    int main()
    cout<<"enter base";
    cout<<"enter exponent";
    for (int i=0;i<100;i++) {cout<<lnumber[i];}
    return 0;

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    4^15 = 2^30. Your machine has integers that are 32 bits.. so they have a total of 2^32 possible values, once you hit 4^16, or 2^32, you just exceeded the maximum. You can try to use unsigned ints, which should let you store 4^16 (but no negative numbers)... but there is no huge difference.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    thank you for your help, but i dont think that is the problem. The digits are stored in the array it should be able to handle 100 digits right? since only basic multiplication and addition are being performed on each element

  4. #4
    Registered User
    Join Date
    Mar 2002
    Unfortunately the following line:

    int lnumber[99]={0};

    declares an array of 99 elements each of type int which are all initialized to 0. Each int in the array can be refered to using an index in the subscript operator like this:


    Later in your program you can assign lnumber[7] a value of 6789 or whatever and lnumber[81] may be assigned the value of 64. Each lnumber[i] has an upper limit as to the value it can hold, 2^32 or whatever. If you want to handle numbers larger than that you can do as previously suggested or change types to type double in your program. If you wish to use VERY large numbers, then you can use a char array to hold individual digits, one digit per element, like this:

    char LargeNumber[100];

    where each LargeNumber[i] will contain a single digit 0-9 or whatever. This is often done in context of a large number class, such that you can write a function to allow the use to routine mathematical operations on such numbers. Until you are ready to do that, however, I suggest using type double instead of type int for your variable/functions. That is what is done with the pow() function in the math (or cmath depending on your compiler) header function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM