It is showing time limit exceeded but why is so

#include

using namespace std;

int digit_sum(int n){
int digit{0},sum{0};
while(n >= 0){
digit=n%10;
n=n/10;
sum=sum+digit;}
return sum;
}

int main()
{
int N{0},i{0};
long long int car_no{0};
cin>>N;

for(i=0;i<N;i++){
        cin>>car_no;
if(digit_sum(car_no)%4 ==0 || digit_sum(car_no)%3==0)
    cout<<"Yes"<<endl;
else
    cout<<"No"<<endl;}

}

Hi @Shubham, inside your digit_sum function , while loop produces an infinite loop because while loop will run till n>=0 and n never becomes negative so it will run indefinitely and u’ll get TLE . instead of n>=0 make it n>0

Also your logic does;nt seems to be correct as you have to check two cases

  1. the sum of odd digits is divisible by 3
  2. the sum of even digits is divisible by 4

if any one of above condition is true then you have to print yes but u are taking sum of all the digits

In case of any doubt feel free to ask :slight_smile:
If you got the answer then mark your doubt as resolved

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.