Thread: Checking Peterson Number

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    24

    Checking Peterson Number

    Greetings everyone, I wrote a program to check if a number is peterson or not and does not seems to be working. Can anyone please check it out.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    int main()
    {
    long int sum=0,f,copy,fact, num; clrscr(); printf("Entre a Number:"); scanf("%ld", &num); num=copy; while(num>0) {
    for(f=num%10, fact=1;f>0;f--) {
    fact=f*fact;
    } sum+=fact; num/=10;
    } if(sum==copy) printf("The Number is piterson number"); else printf("The Number is Not piterson Number"); getch(); return 0;
    }
    Thank you in advance.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    For those who don't know, a Peterson number is one equal to the sum of the factorial of its digits. E.g. 145, because 1! + 4! + 5! = 145.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    My compiler throws a warning on the line:

    Code:
    num=copy;
    which you are obviously ignoring, because there is a BIG problem with that line, but that's not the only problem.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    24
    Thanks to your signature I solved my problem.

    I simply printed num, copy. I found it out that Num was gettting scaned alright but num is not being stored in copy.

    so What I did was, Reversed the code and made it

    Code:
    copy=num;
    now num is being stored in copy instead of copy(which had no assinged value) being stored in num.

    THank you very much.

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    No problem. That's what I was hoping you'd spot.

    Others will complain about you using conio.h and some other things too, but your main problem is "solved".

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    Quote Originally Posted by sunny` View Post
    Thanks to your signature I solved my problem.

    I simply printed num, copy. I found it out that Num was gettting scaned alright but num is not being stored in copy.

    so What I did was, Reversed the code and made it

    Code:
    copy=num;
    now num is being stored in copy instead of copy(which had no assinged value) being stored in num.

    THank you very much.
    good job i was reading your code and was about to point out that problem to you

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    24
    what is the problem with conio.h?

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Not a c standard library header. See conio.h - Wikipedia, the free encyclopedia. Is there a specific reason you are using this? By default I include stdio.h and stdlib.h for most c programming of mine.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    24

    Peterson Numbers in a given range

    Greetings all, I wanted to print all the peterson numbers in a giving range and wrote a program.

    The output is 1 and then it hangs.( for input 1 and 500)

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
     
    long int i,sl,el, sum=0,f,copy,fact=1; clrscr(); printf("Entre a range of Number:"); scanf("%ld%ld", &sl, &el); for(i=sl;i<=el;i++) {
    copy=i; while(i>0) {
    for(f=i%10, fact=1;f>0;f--) fact=f*fact; sum+=fact; i/=10; fact=1;
    } if(sum==copy) printf("%ld\n",sum);
    } getch(); return 0;
    }

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Without taking time to figure out what a Peterson Number is, my first guess is that sum isn't being zeroed for each new number in the outer for loop.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    24
    ok I zeroed the sum but still its not running correctly.

    peterson number is a number say xyz.

    then xyz=x!+y!+z!

    example is 145=1!+4!+5!

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Threads on same subject merged.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by sunny` View Post
    Greetings all, I wanted to print all the peterson numbers in a giving range and wrote a program.

    The output is 1 and then it hangs.( for input 1 and 500)

    Code:
    for(i=sl;i<=el;i++) {
    copy=i; while(i>0) {
    for(f=i%10, fact=1;f>0;f--) fact=f*fact; sum+=fact; i/=10; fact=1;
    } if(sum==copy) printf("%ld\n",sum);
    }
    That line there is your problem. You change i in your while loop, leaving it zero at the end, the your outer for loop does i++ so it's 1, then your while loop makes it 0 again, and you have an infinite loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking if number is prime
    By jackson6612 in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2011, 03:02 PM
  2. Checking for a number
    By a.mlw.walker in forum C Programming
    Replies: 7
    Last Post: 04-20-2009, 03:42 PM
  3. Peterson Gets Death
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-15-2004, 08:16 PM
  4. Scott Peterson - Guilty
    By sean in forum A Brief History of Cprogramming.com
    Replies: 95
    Last Post: 11-15-2004, 11:21 PM
  5. checking for whole number
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 04:55 PM