-2
#include<stdio.h>
int cube(int n)
{
    return (n*n*n);
}/*function to return cubic root*/
int sumcube(int x)
{
    int sum=0,m;
    while(x>0)
    {
        m=x%10;
        sum=sum+(cube(m));
        x=x/10;
    }
    return sum;

}/* returns the sum if cubic digits of an integer*/
int main ()
{
    int i;
    for(i=1; i<=5000; i++)
    {
        if (i==sumcube(i));
        {
            printf("%d\t",i);
        }
    }
}

/* this program is to check wether a number is an armestrong from 1 to 5000 as well as to check the output was all the integers from 1 to 5000*/

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Ahmad ali
  • 3
  • 2
  • What makes you think that the output is not correct? What is the output? – Yunnosch Sep 24 '20 at 21:28
  • What did you do when you found the program didn't work as expected? Did you do any debugging? Run your program in a debugger and/or add debug print statements to trace the execution and examine program state. If you have done that what did you find? Also, please state the expected result and actual result. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – kaylum Sep 24 '20 at 21:28
  • Please read the tag infos of the tags you intend to use. One of those you used was wrong. – Yunnosch Sep 24 '20 at 21:31
  • Have you tried to compile it showing all warnings, e.g. "gcc -Wall"? This should definitely show your problem in if statement. – Nick Sep 24 '20 at 21:38
  • the output was all the integers from 1 to 5000 while the expected values was armestrongs numbers – Ahmad ali Sep 24 '20 at 21:41
  • Surely you can do some debugging. The first incorrect number according to what you have said is `2`. So simply step thru the code line by line as it executes for that input and see where things start to go wrong. Learning to debug effectively is an essential skill and turning to someone else to debug for you should be a very last resort. – kaylum Sep 24 '20 at 21:50
  • See also: https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings – 12431234123412341234123 Sep 24 '20 at 21:57

1 Answers1

1

You have a ; after the if, this is interpreted as an empty body for the if and therefore everything after it will be executed regardless if the condition in the if-statement is true or not.

To avoid the same error in the future, enable all compiler warnings and only disable the warnings you are sure you want to ignore and set the treat warnings as error flag for development (not when you release code, but that is not something you should do at your knowledge level). For gcc use the flags -Wall -Wextra -Wpedantic -Werror. With that, the compiler generates an error for your code.