The function should print numbers that are divisible by 3 in the given range. For example, if I give the range [3,9] it should print 3,6,9 but it prints 0,3,6,9. Why is that? How to fix it?
#include <stdio.h>
int main()
{
printf("enter the value of a \n");
int a;
scanf("%d",&a);
printf("enter the value of b \n");
int b;
scanf("%d",&b);
if(a>b)
{
printf("[%d,%d]\n",b ,a);
for( int b; b<=a; b=b 3)
{
printf("%d\n", b);
}
}
return 0;
}
CodePudding user response:
In this line
for( int b; b<=a; b=b 3)
{
printf("%d\n", b);
}
you are doing int b
which you might know is who you declare variables, that means you are declaring a b inside the block of the loop, and since int's are initialized as 0 by default, your b is always going to be 0. Try removing that redeclaration and see if that fixes your problem
Yes, something like this is still valid:
for(; b<=a; b=b 3)
{
printf("%d\n", b);
}