Help with my code

I need help debugging my code. The formula used for sum is ((no of terms)*(first term+last term))/2
Link to question : -

My Code : -

Well your formula is correct .
As it’s form a AP series so we can also use the formula (n/2)*(2a+(n-1)*d)
where n=no of terms , a=first term , d= step size (equal to the input number).
First we need to find out the first element divisible by a .
For that i checked the condition
if( l %a==0)
first no= l;
k= l %a;
if( (l+k)%a==0)
first no = (l+k)
first no =(l+a-k);
After geeting the first no … we look for the number of elements .
this is done by
{ (r/a) - (l/a) } (if l%a!=0)
{ (r/a)-(l/a)-1 } (if l%a==0)
We have the first no and the number of terms … put it in the formula of AP
n*(2a+(n-1)*d)/2 .
You will get the required sum[.

Here is the link to my solution](

1 Like

thanks for the insight.

1 Like