-1

Where is it logically wrong? I don't find it incorrect but the output it gives is just 1. It should give all the Armstrong number from 1 to 500.

     #include<stdio.h>
     #include<conio.h>
     void main()
         {
         clrscr();
         int a,b,c=0 ,d,i=1;
         while(i<=500)
             {
             b=i;
             while(b>0)
                 {
                 a=b%10;
                 c=(a*a*a)+c;
                 b=b/10;
                 }
             if(c==i)
    `            printf("%d",i);
             i++;
             }
          getch();
          }  
Dweeberly
  • 4,296
  • 2
  • 19
  • 34
  • 2
    First, `void main()` should give a warning, which compiler are you using? second `conio.h` is not a standard header and `getch()` is not a standard function, also `getch()` is not needed in your program, it's needed in your environment, what does that tell you about your environment? – Iharob Al Asimi Jul 04 '15 at 18:33
  • can someone help in findind where the logic or format of the programme is incorrect – anindya Jul 04 '15 at 18:33
  • Use printf statement as much as possible and find where you are missing your logic if you dont know how to use a debugger. – Steephen Jul 04 '15 at 18:36
  • `1` is the only time that `c == i`, try printing `c`. Also, put a space (or other delimiter) after the number inn your `printf`, otherwise you won't know where one number ends and the next starts, `printf("%d ", i);`. Do you know that `d` is not used? – cdarke Jul 04 '15 at 18:51
  • there are only 2 valid (and one optional) way to declare main() all methods return 'int'. strongly compile with all warnings enabled (for gcc, at a minimum use, "-Wall -Wextra -pedantic" ) – user3629249 Jul 04 '15 at 20:15
  • What is an Armstrong number? – sjas Jul 04 '15 at 23:09

2 Answers2

5

You need to initialize c before the inner loop:

 while(i<=500)
 {
     b=i;
     c=0;    /* reset 'c' */
     while(b>0)
     {
        a=b%10;
        c=(a*a*a)+c;
        b=b/10;
     }
}

You are using non-standard signature for main(). See: What should main() return in C and C++?

Community
  • 1
  • 1
P.P
  • 106,931
  • 18
  • 154
  • 210
0

if you run the following code

you will see why there is only one output.

Note: correct declaration of main()

Note: using common functions rather than the proprietary conio.h

Note: uses simple 'for' statement rather than 'while' and increment of 'i'

#include <stdio.h>
#include <stdlib.h>
//#include<conio.h>

int main()
{
    //clrscr();
    int a;
    int b;
    int c=0;
    int i=1;

    for( ; i<=500; i++ )
    {
        b=i;

        while(b>0)
        {
            a=b%10;
            c=(a*a*a)+c;
            b=b/10;
        }

        printf( "a=%d. b=%d, c=%d\n", a,b,c);
        if(c==i)
            printf("%d\n",i);
    } // end for

    //getch();
    getchar();
    return(0);
} // end function main
user3629249
  • 15,593
  • 1
  • 16
  • 17