Thread: Armstrong problem

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    1

    Unhappy Armstrong problem

    I'm a begineer. I wrote this program to write program to write armstrong numbers from 1 to 10000(which should work uptp any number if the range of "i" is increased) but the problem is that it doesn't show one specific armstrong number 153 only. Except that it works like a charm. Please help me ASAP, I have my exams tomorrow...

    insert
    Code:
    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int i, count, ctemp, atemp, rev,paw, total;
        for(i=1; i<10000; i++){
                ctemp=i;
                count=0;
            while(ctemp!=0){
                ctemp=ctemp/10;
                count++;
            }
    
    
            atemp=i;
            total=0;
            rev=0;
            paw=0;
    
    
            while(atemp!=0){
                rev=atemp%10;
                paw=pow(rev,count);
                total= paw+ total;
                atemp=atemp/10;
    
    
            }
           if(total==i){
                printf("%d \n", i);
            }
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    *shrug* works for me (A005188 - OEIS)
    Code:
    $ gcc -Wall -g foo.c -lm
    $ ./a.out 
    1 
    2 
    3 
    4 
    5 
    6 
    7 
    8 
    9 
    153 
    370 
    371 
    407 
    1634 
    8208 
    9474
    Almost certainly, your use of pow() is messing things up with all the int -> float -> int conversions going on.
    Try writing your own integer power() function.

    Also, since you have a really good known test case, you can try debugging
    Code:
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b 21 if ( i == 153 )    // stop at while(atemp!=0) when i reaches 153
    Breakpoint 1 at 0x400689: file foo.c, line 21.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    1 
    2 
    3 
    4 
    5 
    6 
    7 
    8 
    9 
    
    Breakpoint 1, main () at foo.c:21
    21	        while(atemp!=0){
    (gdb) print i
    $1 = 153
    (gdb) n
    22	            rev=atemp%10;
    (gdb) print atemp
    $2 = 153
    (gdb) n
    23	            paw=pow(rev,count);
    (gdb) print rev
    $3 = 3
    (gdb) print count
    $4 = 3
    (gdb) n
    24	            total= paw+ total;
    (gdb) print paw
    $5 = 27
    (gdb) n
    25	            atemp=atemp/10;
    (gdb) n
    21	        while(atemp!=0){
    (gdb) print atemp
    $6 = 15
    (gdb) n
    22	            rev=atemp%10;
    (gdb) 
    23	            paw=pow(rev,count);
    (gdb) print rev
    $7 = 5
    (gdb) print count
    $8 = 3
    (gdb) n
    24	            total= paw+ total;
    (gdb) print paw
    $9 = 125
    (gdb)
    Having reached the problematic iteration, you can single-step and examine variables to see exactly what goes wrong (like I said, it doesn't for me).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. armstrong no.
    By sachin@123 in forum C Programming
    Replies: 1
    Last Post: 03-27-2016, 05:40 PM
  2. Help in find armstrong number
    By san12345 in forum C Programming
    Replies: 1
    Last Post: 12-03-2015, 11:46 AM
  3. armstrong nos.
    By joybanerjee39 in forum C Programming
    Replies: 6
    Last Post: 11-16-2011, 10:48 AM
  4. armstrong problem statement in c++??
    By amerjamil in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2011, 11:02 AM
  5. Printing Armstrong Numbers from 1-500
    By duffmckagan in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:26 AM

Tags for this Thread