having trouble with armstrong program code

This is a discussion on having trouble with armstrong program code within the C Programming forums, part of the General Programming Boards category; program to print armstrongs numbers between 1 and 500. Code: #include<stdio.h> int main() { int m,n,sum=0,a; for(n=1;n<=500;n++) { m=n; while(m!=0) ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    Smile having trouble with armstrong program code

    program to print armstrongs numbers between 1 and 500.

    Code:
    #include<stdio.h>
    int main()
    {
    int m,n,sum=0,a;
    for(n=1;n<=500;n++)
    {
       m=n;
         while(m!=0)
           {
             a=m%10;
             sum=sum+(a*a*a);
             m=m/10;
           }
           if(n==sum)
          printf("%d\n",n);
          
    }
    }
    the above code is not working..please tell me where did it went wrong........

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Presumably you want to reset sum at some point inside your for loop before you start adding to it.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Quote Originally Posted by tabstop View Post
    presumably you want to reset sum at some point inside your for loop before you start adding to it.

    thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. how do I morse code converting program
    By panfilero in forum C Programming
    Replies: 17
    Last Post: 10-29-2005, 09:16 PM
  3. Writing code for a program in C
    By Sure in forum C Programming
    Replies: 7
    Last Post: 06-11-2005, 01:33 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21