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