Why and where my code go wrong?

,

I want to take the lower and upper limit of the range from the user and print all the numbers in between who got same initial and last digit. Also, i have to give the count of such numbers existing.

int a,b,c,d,e,i;
e=0;
printf(“Enter the lower limit”);
scanf(“%d”,&a);
printf(“Enter the upper range”);
scanf(“%d”,&b);

for(i=a; i<=b; i++)
{
    d=i;
    while(i !=0)
    {
        c=i%10;
        i=i/10;
    }
        if(d%10==c)
        {
            printf("%d \n",d);
            e=e+1;
        }
        else
        {
        }
        printf("%d",e);
}

Your code looks fine if I understood it correctly. You may want to move

printf("%d",e);

outside the loop, though.

If you need more help, please give an example. Your input, your output and the output you expected.

The code just doesn’t work and continue to print all numbers till infinity with the numbers having one as it’s last digit.
But what i want it to do is to print all the numbers between a and b who got same inital and last digit. for eg- if input is 90 and 125. Output shall be 99 101 111 121

int main(){
int a,b,c,d,e,i;
e=0;
printf(“Enter the lower limit”);
scanf("%d",&a);
printf(“Enter the upper range”);
scanf("%d",&b);
for(i=a; i<=b; i++)
{
d=i;
while(i !=0)
{
c=i%10;
i=i/10;
}
if(d%10==c)
{
printf("%d \n",d);
e=e+1;
}
i=d; //setting back variable i to its initial value to continue for i++
}
printf("%d\n",e); //printing number of numbers existed in given range
}

1 Like