Thread: whats wrong with my code ..i m trying to print armstrong numbers from 1 to 1000 ..

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    Unhappy whats wrong with my code ..i m trying to print armstrong numbers from 1 to 1000 ..

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    int main()
    {
    int j,number,i,storenumber,sum,power=0,remainder;
    /* number is the list of numbers generated from 1 to 1000, storenumber stores the value of number while its being edited for other purpose,power is the individual digits raised to power equal to total digits in the number */
    for(number=1;number<1001;number++)
    {
    storenumber=number;
    i=0;
      while(number>0)
     {
     number=number/10;
     i=i+1;
    // i simply extracts the number of digits in the given number 
     }
    number=storenumber;
    sum=0;
      for(j=1;j<=i;j++)
      {
      remainder=number%10;
      power=pow(remainder,i);
      number=number/10;
      sum=sum+power;
      }
    //runs the above loop for as many times as the number of digits in the number
    number=storenumber;
        if(sum == number)
       {
       printf("%d is an armstrong number \n",sum);
        }
              
    }
    getch();
    }
    i dont know whats wrong with this code all it prints is that 1 is an armstrong number...pls sum1 point out the mistake
    Last edited by jaikoolwal; 06-22-2010 at 12:37 PM. Reason: changed the code a bit and also put in code tags

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I don't have the time to go over your code, but let me say that it is more likely you will get some help if your program is easy to read (i.e., you need to use code tags to preserve your indentation). You can read about how to use code tags here.

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int j, n, i = 0, k, sum = 0, y = 0, r;
        for (n = 1; n < 1001; n++) {
            k = n;
            while (n > 0) {
                n = n / 10;
                i = i + 1;
    // i simply extracts the number of digits in the given number
            }
            n = k;
            for (j = 1; j <= i; j++) {
                r = n % 10;
                y = pow(r, i);
                n = n / 10;
                sum = sum + y;
            }
    //runs the above loop for as many times as the number of digits in the number
            n = k;
            if (sum == n) {
                printf("%d is an armtrong number \n", sum);
            } else {
                printf(" ");
            }
    
        }
        getch();
    }
    Last edited by kermit; 06-22-2010 at 08:15 AM.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    code tags would be a good start. Enclose your code in tags like

    {code} {/code}

    But replace the braces with square brackets.

    EDIT: nvm, someone got here first.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, try to use variable names that have some meaning. Nobody is going to read through a cryptic program using all the alphabet as different variables. i,j,k are ok for loops but that should be about it.

    Also, your sum variable should be initialized to 0 before computing the sum for each number.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    at above-

    sum is now intialized to zero before computing it for the next number..
    changed variables names to more sensible ones and also introduced code tags...thnx

    now sum1 pls help..its till not working..

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There is a lot of inefficiency in this:

    Code:
    number=storenumber;
    sum=0;
      for(j=1;j<=i;j++)
      {
      remainder=number%10;
      power=pow(remainder,i);
      number=number/10;
      sum=sum+power;
      }
    Remainder, i, power, and number are always the same, so how about:
    Code:
      remainder=number%10;
      power=pow(remainder,i);
      number=number/10;
      sum = power*i;
    No need for the loop at all. Also the use of storenumber and number is silly. Just copy it into some other variable and use that instread of saving it in a different variable and resetting it.

    However, the problem is that your formula for Armstrong numbers is wrong, as hopefully is clearer now I have simplified your operations.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by MK27 View Post
    There is a lot of inefficiency in this:

    Code:
    number=storenumber;
    sum=0;
      for(j=1;j<=i;j++)
      {
      remainder=number%10;
      power=pow(remainder,i);
      number=number/10;
      sum=sum+power;
      }
    Remainder, i, power, and number are always the same, so how about:
    Code:
      remainder=number%10;
      power=pow(remainder,i);
      number=number/10;
      sum = power*i;
    No need for the loop at all. Also the use of storenumber and number is silly. Just copy it into some other variable and use that instread of saving it in a different variable and resetting it.

    However, the problem is that your formula for Armstrong numbers is wrong, as hopefully is clearer now I have simplified your operations.
    thnx M2K...but if i do not use a loop where you have suggested me not to then it wont be able to extract all the individual digits..for example if the number is 371 it will only extract the remainder as 1 (last digit ) and move forward after that...i have initialized 'i' before the loop in my program and now it is printing all armstrong numbers except 153...pls suggest...thnx

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm really sorry, I must have been ignoring this line in my analysis:
    Code:
      number=number/10;
    Which does make the loop purposeful and my reduction of it incorrect! (In fact, I wrote that post, then deleted it, then posted it again -- presumably I noticed my mistake and then stupidly forgot it...)

    Anyway, I didn't mean you don't need a loop, I meant I thought the loop you were using was incorrect, since it didn't do much (but that was my mistake).

    It could still be incorrect for calculating the armstrong number although it is probably close. I would do this:
    Code:
            if (sum == n) {
                printf("%d is an armtrong number \n", sum);
            } else {
                printf("n = %d  but sum = %d\n",n,sum);
            }
    This will enable you to see (if you have a list of the correct armstrong numbers) where you are going wrong and hopefully, why.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    @MK27-thnx for the help ...i appreciate it

    well now all of my code is working and it is also printing all the armstrong numbers except for 153...i wonder why...ny suggestions ny1??

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jaikoolwal View Post
    @MK27-thnx for the help ...i appreciate it

    well now all of my code is working and it is also printing all the armstrong numbers except for 153...i wonder why...ny suggestions ny1??
    Did you try adding that printf? Check what it says the sum for is when n =153. Obviously you are very close.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    How about this//the program find the armstrong number from 1 to 1000)

    Code:
    for(num=1;num<1001;num++)
    {
    temp_num=num;
    sum=0;
    sprintf(str,"%d",temp_num);
    power=strlen(str);
    
    while(temp_num)
    {
    	sum+=(int)pow(temp_num%10,power);
    	temp_num/=10;
    }
    
    if(sum==num)
    printf("Armstrong number: %d\n",num);
    }
    I don't know whether it is right or not?Need your help.

  12. #12
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by MK27 View Post
    Did you try adding that printf? Check what it says the sum for is when n =153. Obviously you are very close.
    yeah..i did try it...i also added a printf which prints the number currently being operated on in the loop..but then the output become really weird...it prints the number and the sum of its digits starting from the number 701 to 1000 and finally prints that 1 is an armstrong number !!!

    weird code !!

    S.O.S

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I made it to new Armstrong code to print up to 10 digit.
    By hardip patel in forum C Programming
    Replies: 4
    Last Post: 06-02-2010, 02:56 PM
  2. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  3. What's wrong with my Win32 Wrapper code?
    By miica in forum Windows Programming
    Replies: 9
    Last Post: 02-22-2005, 08:55 PM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. What is wrong here, just won't print the correct numbers
    By Unregistered in forum C++ Programming
    Replies: 16
    Last Post: 05-10-2002, 03:31 PM