Thread: Logical error in Armstrong number code

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    12

    Logical error in Armstrong number code

    Code:
    #include<stdio.h>    //to print armstrong no between 100 to 500
       int main()
            {
            int a,n,i,j,c;
         for(j=100;j<=500;j++)
               {
                    int d=0;
                    n=100;
                    n++;
             a=n;
                       for(i=1;i<=3;i++)
                       {
                        c=a%10;
                       a=a/10;
    
                       d=d+c*c*c;
                       }
                    if(d==n)
                    printf("%d armstrong no\n",d);
                }
                return 0;
                }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What's wrong with your program, besides having poor indentation and variable names?

    You need to give us more detail. What output do you get when you run it? What output are you expecting?

    Also, using better variable names that describe what each variable is used for, would be helpful. That would let both you and us more easily understand what the logic in your code is doing, and possibly spot mistakes. For example, c could be called digit, d could be called digit_sum, etc.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    12
    program is running successfully but it is not printing armstrong no. between 100 to 500
    It's not printing anything.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    12
    Code:
    #include<stdio.h>
       int main()
       {
       int temdigit,digit,i,j,c;
       for(j=100;j<=500;j++)  // for running code 500 times
          {
           int d=0;   
           digit=100;           
           digit++;
           temdigit=digit;
           for(i=1;i<=3;i++)
               {
               c=a%10;  // c= last digit of the number
               a=a/10;   //a=remaining digit of the number
    
               d=d+c*c*c;   //d= total sum of the cubes of the digit
               }
          if(d==n)       //checking whether total sum ==original number
          printf("%d armstrong no\n",d);
    
          }
          return 0;
       }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So how do you know if it's running successfully if it's not printing the numbers? That doesn't sound successful to me. However, it prints them just fine for me:
    Code:
    $ ./foo
    153 armstrong no
    370 armstrong no
    371 armstrong no
    407 armstrong no
    EDIT: Sounds like a IDE config issue. Perhaps look here: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    n undeclared
    a undeclared

    Don't forget to rename your variables in your code.

    Your variable digit gets the value 101 every loop and does not change.
    Code:
    digit=100;
    digit++;
    Therefore your program will only print stuff if d is equal to 101
    Code:
    if(d==digit)       //checking whether total sum ==original number
      printf("%d armstrong no\n",d);
    You need to put this before your first for loop:
    Code:
    digit=100;
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serious logical error!
    By Mukul Kumar in forum C++ Programming
    Replies: 12
    Last Post: 11-18-2013, 07:12 PM
  2. Logical Error
    By Luigi1 in forum C Programming
    Replies: 1
    Last Post: 02-27-2011, 02:41 PM
  3. Replies: 11
    Last Post: 06-23-2010, 01:36 AM
  4. 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
  5. having trouble with armstrong program code
    By cooldude in forum C Programming
    Replies: 2
    Last Post: 09-06-2009, 11:58 PM

Tags for this Thread