# Can anyone provide a test case where my code fails for Divisible by 8 problem in starters 103

//My algorithm is as follows:
//1. if n==1 return 8
//2. if(n>=2) we need not change any digit if already the number is divisible by 8. we can change the digit in one’s place as per ten’s place digit. Thus max required changes are one.

include <bits/stdc++.h>
using namespace std;
define ll long long int

int main()
{
ll t;
cin >> t;
while (t–)
{
int n;
cin >> n;
string s;
cin >> s;

``````    if (s.length() == 1)
{

cout << 8 << endl;
}

else if (s.length() == 2 || (s.length() > 2 && (s[n - 3] - '0') % 2 == 0))
{
if (s[n - 2] == '0')
{
if (s[n - 1] != '0')
{
s[n - 1] == '8';
}
}
else if (s[n - 2] == '1')
s[n - 1] = '6';
else if (s[n - 2] == '2')
s[n - 1] = '4';
else if (s[n - 2] == '3')
s[n - 1] = '2';
else if (s[n - 2] == '4')
{
if (s[n - 1] != '0')
{
s[n - 1] = '8';
}
}
else if (s[n - 2] == '5')
s[n - 1] = '6';
else if (s[n - 2] == '6')
s[n - 1] = '4';
else if (s[n - 2] == '7')
s[n - 1] = '2';
else if (s[n - 2] == '8')
{
if (s[n - 1] != '0')
{
s[n - 1] = '8';
}
}
else if (s[n - 2] == '9')
{
s[n - 1] = '6';
}

cout << s << endl;
}
else
{

if (s[n - 2] == '0')
{
s[n - 1] = '4';
}
else if (s[n - 2] == '1')
s[n - 1] = '2';
else if (s[n - 2] == '2')
{
if (s[n - 1] != '0')
{
s[n - 1] = '8';
}
}
else if (s[n - 2] == '3')
s[n - 1] = '6';
else if (s[n - 2] == '4')
{
s[n - 1] = '4';
}
else if (s[n - 2] == '5')
s[n - 1] = '2';
else if (s[n - 2] == '6')
{
if (s[n - 1] != '0')
{
s[n - 1] = '8';
}
}
else if (s[n - 2] == '7')
s[n - 1] = '6';
else if (s[n - 2] == '8')
{
s[n - 1] = '4';
}
else if (s[n - 2] == '9')

{
s[n - 1] = '2';
}

cout << s << endl;
}
}
return 0;
``````

}

@dilip0409
plzz refer the following solution for better understanding of the logic.

``````#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int n,v;
string s;
cin>>n>>s;
if(n<=3) v=stoi(s);
else v=stoi(s.substr(n-3));
if(v%8==0) cout<<s<<endl;
else{
int ans;
if(n==1) ans=0;
else if(n==2) ans=stoi(s.substr(n-2,1));
else ans=stoi(s.substr(n-3,2));
if(ans%4==0) s.back()='8';
else if(ans%4==1) s.back()='6';
else if(ans%4==2) s.back()='4';
else s.back()='2';
cout<<s<<endl;
}
}
return 0;
}
``````