-1
#include <stdio.h>

int main() {
  int n;

  do {
    printf("Enter a Number :");

    scanf("%d", &n);

    printf("%d \n", n);

    if (n % 7 == 0) {
      break;
    }
  } while (1);

  printf("Program Ends");

  return 0;
}

Why the problem run for infinite time for input of any character?

I want to know why it is happening? It should break from the loop because character is not divisible by 7?

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • Check return value of `scanf`. Read from docs (like, just google "C scanf") what the return value means. – hyde Nov 24 '22 at 18:09
  • 1
    Does this answer your question? [Why is scanf() causing infinite loop in this code?](https://stackoverflow.com/questions/1716013/why-is-scanf-causing-infinite-loop-in-this-code) – OznOg Nov 24 '22 at 18:11

1 Answers1

1

It doesn't read a character. This call: scanf("%d", &n); doesn't do anything because there is no number to read. It returns 0 to let you know it didn't read a number, but you don't check.

user253751
  • 57,427
  • 7
  • 48
  • 90