-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?

2
  • Check return value of scanf. Read from docs (like, just google "C scanf") what the return value means. Commented Nov 24, 2022 at 18:09
  • 1
    Does this answer your question? Why is scanf() causing infinite loop in this code? Commented Nov 24, 2022 at 18:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.