-5

This is the code for checking Armstrong number. I don't see any error. Please help.

#include<stdio.h>
int main(){
   int no;
   int k, sum=0;
   printf("Enter the no: ");
   scanf("%d",&no);
   while(no>0){
     k=no%10;
     sum=sum+(k*k*k);
     k=k/10;
   }
   if(sum==no)
     printf("The no is armstrong");
    else
     printf("The no is not armstrong");
   return 0;
}

I tried to write a code for finding Armstrong no (like 153- 1*1*1+5*5*5+3*3*3).and the program doesn't generate any error, but it also doesn't show the output. I am using VS code as editor.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Welcome to SO. Please do not post links to pictures of code. Code is text. Add it as formatted text directly in your question. Same holds true for your input, output, expected output and any error messages or warnings. – Gerhardh Feb 28 '23 at 14:46
  • Code should be text, in the question. It's also easier to copy/paste text than it is to cap/crop/upload/link code. Code as text also allows people to run it themselves, as it can be easily copy/pasted around. – sweenish Feb 28 '23 at 14:47
  • `stdout` is normally line bufferd. To trigger flusing `stdout` you can either use `fflush(stdout);` or add `\n` to your printf strings. – Gerhardh Feb 28 '23 at 14:48
  • 1
    Please do not use images for your code snippets as this may make it harder for people to help you, instead try using [code blocks](https://stackoverflow.com/help/formatting/). – xihtyM Feb 28 '23 at 14:48
  • @Gerhardh Upon program termination, they should be flushed anyways. – Sourav Ghosh Feb 28 '23 at 14:48
  • @SouravGhosh true but the program might be waiting at `scanf` call. – Gerhardh Feb 28 '23 at 14:49
  • If your code is compiling you should also consider using a debugger such as gdb. – xihtyM Feb 28 '23 at 14:49
  • When do you think your `while` loop should exit? You never change `no` in the loop. – Gerhardh Feb 28 '23 at 14:50
  • 2
    The while loop checks `no` is greater than 0, but nothing ever changes `no. – doctorlove Feb 28 '23 at 14:50
  • Start using a **debugger**. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jason Feb 28 '23 at 14:52
  • Also note that your program does not 'stop' - quite the opposite. If your app stops responding, check 'ps' or "Task Manager' to see if your code is stopped/blocked, (0% CPU use), or looping, (using 100% of a core). – Martin James Mar 01 '23 at 08:33

1 Answers1

5

You have a while loop depending on the value of no, but you never change the value of no inside the loop.

Essentially, you're running into infinite loop. Double-check the logic.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261