# Thread: my nifty little exponent program

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 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)
{diff=fnum*fnum2-k;carry=k/10;break;}
}

}
//////////////////////////////////////////////

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++)
{mult(farr2,fbase);}
}
////////////////////////////////////////

#include <iostream>
using namespace std;

int main()
{
cout<<"enter base";
cin>>base;
cout<<"enter exponent";
cin>>mypow;
lnumber[99]=base;
power(lnumber,mypow,base);
for (int i=0;i<100;i++) {cout<<lnumber[i];}
return 0;
}

2. 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.

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

4. 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:

lnumber[7]

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.