Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my code not giving any output even though there is no error in the code?

I am new to C. I have to initialise ‘a’ with 0 , required output is natural number from 10 to 20 using while loop but getting no output even though there is no error in the code.

#include <stdio.h>

int main()
{
    int a=0;
    while (a<=20) {
      if (a>=10) {
        printf("The Value of A is %d\n",a);
        a++;
      }
    }
    return 0;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The a variable is zero, which allows you to enter the loop.

Unfortunately, it’s only incremented if it’s already greater than or equal to ten, which it never is, because it’s only incremented if it’s already greater than or equal to ten, which it never is … and so on, ad infinitum.

You should probably move the a++ to after the closing brace of the if statement. That way, it will be incremented regardless of its current value:

#include <stdio.h>

int main(void) {
    int a = 0;
    while (a <= 20) {
        if (a >= 10) {
          printf("The Value of A is %d\n", a);
                   // <- NOT HERE,
        }          //
        a++;       // <- BUT HERE.
    }
    return 0;
}

The output of that is closer to what you seem to want:

The Value of A is 10
The Value of A is 11
The Value of A is 12
The Value of A is 13
The Value of A is 14
The Value of A is 15
The Value of A is 16
The Value of A is 17
The Value of A is 18
The Value of A is 19
The Value of A is 20
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading