Last Digit Sum(http://www.codechef.com/COOK14/problems/LASTDIG)

I have spen hours on this code figuring out what went wrong.I am not getting why this is not showing the output.

 # include <stdio.h>
 #include <conio.h>
 int main()
 {
int a,i,j,dig,sum1=0,sum2=0,k,b,c,o;

    scanf("%d",&a);      
  for(k=0;k<a;k++)             // For test cases
  {
    scanf("%d",&b);
    scanf("%d",&c);
    for(j=b;j<=c;j++)         //scanning the two numbers.
    {  
    while(j!=0)
    {sum1=0;
    dig=j%10;
    if(dig%2==0){sum1=(sum1+(2*dig));}
    else{sum1=(sum1+dig);}                    //finding the sum of digits.If digit is even 
    j=j/10;                                       multiply it by 2 and add if it is odd simply 

` } just add

    o=sum1%10;      
    sum2=sum2+o;                             //finding last digit of the sum.

    }
    printf("%d",sum2);                      //printing sum2 which is sum of all the last digits 
    sum1=0;                                  of sum1
    }          
    getch();
    return 0;

}

what is the question code?

in the for loop,you use j as counter…
and in its inner while loop you change the value of j…
by j=j/10;
moreover
you have to initialise sum1 and sum2 inside the test case loop…
so that they should be zero for every new case.

you have to remove conio and getch as well

ok thnx…:slight_smile:

Pls can you correct it for me,.

include <stdio.h>

// #include <conio.h>
int main()
{
long long int a,i,j,dig,sum1=0,sum2=0,k,b,c,o,n;

scanf("%lld",&a);

for(k=0;k<a;k++) // For test cases
{
scanf("%lld",&b);
scanf("%lld",&c);
sum2=0;
for(j=b;j<=c;j++) //scanning the two numbers.
{
n=j;
sum1=0;
while(n!=0)
{
// sum1=0;
dig=n%10;

if(dig%2==0)
sum1=(sum1+(2*dig));
else
sum1=(sum1+dig);                    //finding the sum of digits.If digit is even

n=n/10;                                 //      multiply it by 2 and add if it is odd simply
 }
o=sum1%10;
sum2=sum2+o;                             //finding last digit of the sum.

}
printf("%lld\n",sum2);                      //printing sum2 which is sum of all the last digits
sum1=0;                 //                 of sum1
}

// getch();
return 0;

}
vote my ans :slight_smile: