I have seen similar posts but could not find the exact answer that i can understand.

I have to store and print the value which will 2 power 5 digit number.

ex:

2 ^ 55555

2 ^ 99999

etc ...

Please help me

**coding:** C**Compiler:** DevC++

0

You'll need to write your own data type to do this. There is also a library out there which can hold numbers up to a stupid amount, but I can't rememeber what it is called. If you have a search on Google I'm sure you'll find it.

EDIT:

Found it, it's the GMP library -> http://gmplib.org/

*Edited
by Auraomega*: n/a

0

I'm a newbie and only know simple programming.

Yes i already been to that GMP site and i was completely lost. I mean i dont understand anything there.

Isn't there any simple logic to this ?

Some people said to use arrays to store, but i haven't got the idea to do so...

0

Some people said to use arrays to store, but i haven't got the idea to do so...

Yes, you will use an array. The first element (at index 0, eg. number[0]) will store the number of digits your number has. The next ones will store the actual number.

0

You might look at it as an array of integers. Each digit in the number, then is assigned to an element of the array:

456 becomes a[4][5][6] in it's first three elements, or,

a[0] = 4, a[1] = 5, and a[2] = 6.

You can do about the same thing, using a char array, but it's just a bit more complicated.

You will have to keep track of how many digits you have in your array OR you could give your last (highest) array element, a number like MAXINT (which is the maximum value that your integer can have. You need to add the include file <valuess.h> to your program, in my case. Check with your compiler and see what name your maximum integer value, goes by: MAX_INT is also popular, and what header file you need to include, to use that value.

EDIT: I like Creeps idea for determining how many digits belong to the number, also. Very nice. :)

Now, if I have a value like 18 trillion, I can add it to my int array, by peeling off the digits, one at a time, until all the digits are used up:

How could I use the mod operator, %, and division by 10, to "peel" off digits from the right hand side of a number, one at a time, in a while loop?

*Edited
by Adak*: n/a

0

You can also use string variable that means char pointer

`char *ch;`

to store large data but problem is that on using this, is that you have to manipulate data by your own function.Try to solve or give me some time I will give some function to manipulate these type of data.

Best of luck.

0

Thank you very much for the responses.

And sorry for being such a noob, but my problem is that:

I have to write a program to read an integer of 5 digits, say N.

I then have to print the (2 power N) value.

so 2 ^ 99999 happens to be a very large number.

How can i even consider the output value ?

Storing a known value in array seems possible, but the output of a calculation to be stored is something i cant understand.

And if the solution to this problem has been discussed above, then i must be a real nut, coz i didnt feel l found it :(

0

We got off on the wrong track, I think - you tricked us!

What notation for the answer, can you use? Because I don't think your teacher wants all those digits being printed out, if you printed out the answer in base 10 (like a normal number).

So maybe engineering notation, would be OK? printf() will format an answer for engineering notation output.

So MAYBE the assignment is not to do a lot with a big integer array, maybe it's to set up printf() to print out the answer, with the right format?

If I said that in binary, the columns (or "places") have the following values:

2^5, 2^4, 2^3, 2^2, 2^1, 2^0

would that stir up any idea's for you?

Can you post some examples from your class notes? That would be best. The details of the way the assignment is phrased, could make all the difference here.

0

All i'm asked is this:

Write a program that takes a 5 digit number and calculates 2 power

that number and prints it. Program should not use the Big-integer and Exponential Function's.

And the code so far:

```
#include<stdio.h>
#include<conio.h>
main()
{
while(1){
long int res=1,n=0,i=0;
printf("please enter 5 digit no:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
res=(res*2);
}
printf("Result is %d \n",res);
getch();
}
}
```

The above code works for maximum of **n=30**

when i used **unsigned double** and **%ld **the output is always ZERO.

0

Sorry, When I try to solve your i just forget, that you ask me problem in c and I just solve it c++ so if you have knowledge of then you use and enjoy other wise tell me I will change in c. Here is my code showing how to get input,output and addition by using operator overloading

```
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h> //for ltoa()
const SZ=200;
class verylong
{
private:
char vlstr[SZ];
int vlen;
public:
verylong(){vlstr[0]='\0';vlen=0;}
verylong(char s[SZ]){strcpy(vlstr,s); vlen=strlen(s);}
verylong(unsigned long n){ltoa(n,vlstr,10);strrev(vlstr);vlen=strlen(vlstr);}
void putvl();
void getvl();
verylong operator + (verylong);
//verylong operator * (verylong);
};
void verylong::putvl()
{
char temp[SZ];
strcpy(temp,vlstr);
cout<<strrev(temp);
}
void verylong::getvl()
{
cin>>vlstr;
vlen=strlen(vlstr);
strrev(vlstr);
}
verylong verylong::operator +(verylong v)
{
char temp[SZ];
int maxlen=vlen;
maxlen=(vlen>v.vlen)?vlen:v.vlen;
int carry=0;
for(int j=0;j<maxlen;j++)
{
int d1=(j>vlen-1) ? 0:vlstr[j]-'0';
int d2=(j>v.vlen-1)?0:v.vlstr[j]-'0';
int digitsum=d1+d2+carry;
if(digitsum>=10){digitsum-10;carry=1;}
else
carry=0;
temp[j]=digitsum+'0';
}
if(carry==1)
temp[j++]='1';
temp[j]='\0';
return verylong(temp);
}
void main()
{
verylong a1;
verylong a2;
a1.getvl();
a2.getvl();
a2=a1+a2;
a2.putvl();
getch();
}
```

Try other mathematical function

best of luck

0

@**Praveen** SO thank you, but i dont understand C++ , read the code twice but i dont understand the :: and << and many different operators :(

i would like to understand the logic, i mean i want to learn technique to handle it.

if i was asked the question then there should have been an answer to it.

And i see that many have asked the same exact question in net, a little google show lots of SAME questions and even some interview questions have it, but i didnt find even one answer, some give in java and some in ohter :(

*Edited
by firoz3321*: n/a

1

Converted Code In c

```
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h> //for ltoa()
struct verylong
{
char vlstr[200];
int vlen;
};
struct verylong add(struct verylong a1,struct verylong a2);
struct verylong getvl();
void main()
{
struct verylong a1;
struct verylong a2;
struct verylong a3;
char vlstr[200];
clrscr();
a1=getvl();
a2=getvl();
a3=add(a1,a2);
printf("\n%s",strrev(a3.vlstr));
getch();
}
struct verylong getvl()
{
struct verylong a1;
gets(a1.vlstr);
a1.vlen=strlen(a1.vlstr);
strrev(a1.vlstr);
return a1;
}
struct verylong add(struct verylong a1,struct verylong a2)
{
char temp[200];
int maxlen=a1.vlen;
int carry=0;
int d1,d2,digitsum,j;
maxlen=a1.vlen;
maxlen=(a1.vlen>a2.vlen)?a1.vlen:a2.vlen;
for(j=0;j<maxlen;j++)
{
d1=(j>a1.vlen-1) ? 0:a1.vlstr[j]-'0';
d2=(j>a2.vlen-1)?0:a2.vlstr[j]-'0';
digitsum=d1+d2+carry;
if(digitsum>=10)
{
digitsum-=10;
carry=1;
}
else
carry=0;
temp[j]=digitsum+'0';
}
if(carry==1)
temp[j++]='1';
temp[j]='\0';
strcpy(a2.vlstr,temp);
return a2;
}
```

Now check this post is in **c **, try to understand logic now if you get trouble point out that and ask Q.

Best Of Luck firoz3321

0

wow you are so quick :)

Thank you

I'll try to get the full understanding of it :) Thank you again

0

Hi, here I use three structure variable structure these are

```
struct verylong a1;
struct verylong a2;
struct verylong a3;
```

a1 and a2 accept input from user and store in it.

and a function add is use to store the data after addition..........

`struct verylong add(struct verylong a1,struct verylong a2)`

add function take two structure variable as a parameter and return a structure variable. As I understand your problem is to calculate the power. For calculating power you have to create function for multiplication as I one show for addition after creating multiplication the as many as the power.

Hope you understand what I try to explain.....work for that for some time other wise I will send.........

Best of luck.

1

I think still you waiting for my reply. No problem take it what you want

```
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h> //for ltoa()
struct verylong
{
char vlstr[200];
int vlen;
};
struct verylong mul(struct verylong a1,struct verylong a2);
struct verylong mult10(struct verylong a2);
struct verylong add(struct verylong a1,struct verylong a2);
struct verylong multdigit(struct verylong a1,int d2);
struct verylong getvl();
void putvl(struct verylong a3);
void main()
{
struct verylong a1;
struct verylong a2;
int n,i;
clrscr();
printf("\nEnter n (2 ^ n) :");
scanf("%d",&n);
a1.vlstr[0]='2';
a1.vlstr[1]='\0';
a1.vlen=strlen(a1.vlstr);
a2=a1;
while(n-1)
{
a2=mul(a1,a2);
n--;
}
strrev(a2.vlstr);
for(i=strlen(a2.vlstr)-1;i>=0;i--)
{
if(a2.vlstr[i]=='0') a2.vlstr[i]='\0';
else break;
}
printf("\n%s",a2.vlstr);
getch();
}
void putvl(struct verylong a3)
{
char temp[200];
strcpy(temp,a3.vlstr);
printf("\n%s",strrev(temp));
}
struct verylong getvl()
{
struct verylong a1;
gets(a1.vlstr);
a1.vlen=strlen(a1.vlstr);
strrev(a1.vlstr);
return a1;
}
struct verylong add(struct verylong a1,struct verylong a2)
{
char temp[200];
int maxlen=a1.vlen;
int carry=0;
int d1,d2,digitsum,j;
maxlen=a1.vlen;
maxlen=(a1.vlen>a2.vlen)?a1.vlen:a2.vlen;
for(j=0;j<maxlen;j++)
{
d1=(j>a1.vlen-1) ? 0:a1.vlstr[j]-'0';
d2=(j>a2.vlen-1)?0:a2.vlstr[j]-'0';
digitsum=d1+d2+carry;
if(digitsum>=10)
{
digitsum-=10;
carry=1;
}
else
carry=0;
temp[j]=digitsum+'0';
}
if(carry==1)
temp[j++]='1';
temp[j]='\0';
strcpy(a2.vlstr,temp);
a2.vlen=strlen(a2.vlstr);
return a2;
}
struct verylong mul(struct verylong a1,struct verylong a2)
{
struct verylong pprod;
struct verylong tempsum;
int j,digit,k;
tempsum.vlstr[0]='0';
tempsum.vlstr[1]='\0';
tempsum.vlen=1;
for(j=0;j<a2.vlen;j++)
{
digit=a2.vlstr[j]-'0';
pprod=multdigit(a1,digit);
for(k=0;k<=j;k++)
pprod=mult10(pprod);
tempsum=add(pprod,tempsum);
}
tempsum.vlen=strlen(tempsum.vlstr);
return tempsum;
}
struct verylong mult10(struct verylong a2)
{
struct verylong temp;
int j;
for(j=a2.vlen-1;j>=0;j--)
temp.vlstr[j+1]=a2.vlstr[j];
temp.vlstr[0]='0';
temp.vlstr[a2.vlen+1]='\0';
temp.vlen=strlen(temp.vlstr);
return temp;
}
struct verylong multdigit(struct verylong a1,int d2)
{
struct verylong temp;
int carry=0,j,d1,digitprod;
for (j=0;j<a1.vlen;j++)
{
d1=a1.vlstr[j]-'0';
digitprod=d1*d2;
digitprod+=carry;
if(digitprod>=10)
{carry=digitprod/10;
digitprod-=carry*10;
}
else
carry=0;
temp.vlstr[j]=digitprod+'0';
}
if(carry!=0)
temp.vlstr[j++]=carry+'0';
temp.vlstr[j]='\0';
temp.vlen=strlen(temp.vlstr);
return temp;
}
```

It help you but try understand the logic ok.

Best Of Luck

0

I basically left this code because i was not gettin it :(

And thanks for the code, but the code odesnt work in my system for even 3 digit numbers and i was asking for the 5 digit number...

0

Just change 200 with 1000 it will be work try to do some thing by you self........

Best Of Luck.

0

Before living I want tell you some thing about this that just image in resultant number of digit come after calculating c^12345 (five digit) according to that change change 200 to that digit, but mind it, it will not efficient programming to do like this, because it take may be few min. to show the result but it will definitely work.

I check this program after changing 200 to 2000 for following input 2^666, and it take 2 to 3 min. but it work.

*Edited
by prvnkmr449*: n/a

This topic has been dead for over six months. Start a new discussion instead.

Recommended Topics

how can i auto sum the values in datagrid cells of the same row in vb.net

I have been trying to figure this out for hours with no avail. Basically I need to print out user input as Initial value, ASCII(char), ASCII(int), and Hex. I ...

- Your program should read the input files by using Unix shell redirection (e.g. a.out<lab1.dat) to read the input from

stdin (C). By using redirection, it is unnecessary to open ...