Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,167 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,882 people online right now. Registration is fast and FREE... Join Now!




Error: pow overflow

 
Reply to this topicStart new topic

Error: pow overflow

symidran
13 Dec, 2007 - 10:57 AM
Post #1

New D.I.C Head
*

Joined: 17 Feb, 2006
Posts: 25


My Contributions
Hi all,
please look at the code below, its to generate all such numbers which have the sum of factorial of individual digits equal to the number itself (eg 145=1!+4!+5!)
CODE

#include<iostream.h>
#include<conio.h>
#include<math.h>

long facto(long n1) {
if(n1==0||n1==1)
return 1;
else
return n1*facto(n1-1);
}

void check(long num) {
long temp,t,x,i,sum=0;
for(i=0;(num<=pow(10,i));i++) {       // to get the total no of digits
}
t=i-1;
for(i=t;i>=0;i--) {                             //to get each individual digit
x=pow(10,i);                                    
temp=num/x;
sum+=facto(temp);
num%=x;
}
cout<<"\nSUM"<<sum;
if(sum==num)
cout<<num;
}


void main() {
cout<<"\nThe numbers for which the factorial of digits is equal to its value are : \n";
for (long a=1;a<100000;a++) {
check(a);
}
getch();
}


Whats the mistake i am doing, i getting pow overflow error, and then TC crashes . Doesnt work on BCC 4.5 either.
Please help.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Error: Pow Overflow
13 Dec, 2007 - 12:23 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,446



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Why are you using such an old compiler? You can download BCC 5.5 for free.
User is online!Profile CardPM
+Quote Post

symidran
RE: Error: Pow Overflow
13 Dec, 2007 - 01:18 PM
Post #3

New D.I.C Head
*

Joined: 17 Feb, 2006
Posts: 25


My Contributions
QUOTE(no2pencil @ 13 Dec, 2007 - 10:23 AM) *

Why are you using such an old compiler? You can download BCC 5.5 for free.

dont think its compiler error, i feel there 's some logic going wrong

User is offlineProfile CardPM
+Quote Post

symidran
RE: Error: Pow Overflow
13 Dec, 2007 - 10:32 PM
Post #4

New D.I.C Head
*

Joined: 17 Feb, 2006
Posts: 25


My Contributions
Help plz

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Error: Pow Overflow
14 Dec, 2007 - 04:01 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Well, where num=1, for(i=0;(num<=pow(10,i));i++) is an infinite loop. That's the most likely problem.

Also, you really don't need pow for this. Just get the last digit with a mod 10 and then divide the working number by 10.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

symidran
RE: Error: Pow Overflow
14 Dec, 2007 - 05:22 AM
Post #6

New D.I.C Head
*

Joined: 17 Feb, 2006
Posts: 25


My Contributions
Yes ...thanks for finding the mistake, indeed an infinite loop.

QUOTE(baavgai @ 14 Dec, 2007 - 02:01 AM) *


Also, you really don't need pow for this. Just get the last digit with a mod 10 and then divide the working number by 10.



Could you plz explain this in more detail

Thx a lot
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Error: Pow Overflow
14 Dec, 2007 - 05:35 AM
Post #7

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(symidran @ 14 Dec, 2007 - 08:22 AM) *

Could you plz explain this in more detail


Probably not without writing the code, but we'll give it a go.

int n = 123;
int digit = n % 10; // this is now 3, the remainder from dividing n by 10
n = n /10; // this is now 12, because it's an integer, the .3 is dropped.

If you repeat this process, you can chop n down to zero and have digit hold the last number each time. No pow.


User is offlineProfile CardPM
+Quote Post

symidran
RE: Error: Pow Overflow
14 Dec, 2007 - 08:32 AM
Post #8

New D.I.C Head
*

Joined: 17 Feb, 2006
Posts: 25


My Contributions
QUOTE(baavgai @ 14 Dec, 2007 - 03:35 AM) *

QUOTE(symidran @ 14 Dec, 2007 - 08:22 AM) *

Could you plz explain this in more detail


Probably not without writing the code, but we'll give it a go.

int n = 123;
int digit = n % 10; // this is now 3, the remainder from dividing n by 10
n = n /10; // this is now 12, because it's an integer, the .3 is dropped.

If you repeat this process, you can chop n down to zero and have digit hold the last number each time. No pow.

WOW ! Thx a lot, got it working now smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:07AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month