• + 1 comment

    i need help. my code is running correctly. but when i try to print the sum of k-number, it gives wrong answer..

    int main()
    {
    int l,r,k,i,sum=0;
    
    scanf("%d%d%d",&l,&r,&k);
    for(i=l;i<=r;i++)
    {   
    
        int x=i,cnt=0,a=k,b,c=i,d=0,e,f=i,g,w;
    
        while(x!=0)
        {
            cnt++;
            x=x/10;
    
        }
    
        if(cnt<=k)
        {
            sum=sum+i;
    
        }
    
    
        else
        {
            while(a!=0)
            {
                b=c%10;
                d=d+b;
                a--;
                c=c/10;
            }
    
            while(cnt!=0)
            {
                if(cnt<=k)
                {
                    e=f%10;
                    g=g+e;  
                }
                f=f/10;
                cnt--;
            }
    
    
    
            if(d==g)
            {
                sum=sum+i;
            }
        }
    }
    
    printf("%d",sum);   /*when i include this line in my code,                          it will give wrong answer.*/
    return 0;
    }