#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
   // cout << "Hello world!" << endl;
    int t,i,j,a,flag;
    char str[1000001];
    cin>>t;
    while(t--)
    {
        a=1;
        scanf("%s",str);
        for(i=0;i<=strlen(str);i++)
        {flag=0;
            if(!isdigit(str[i]))
            {flag=0;
                for(j=i-1;isdigit(str[j]);j--)
                {
                    if(str[j]!='0')
                    {
                        flag++;
                        a*=str[j]-'0';
                        //cout<<"flag"<<flag<<" "<<a<<"A"<<endl;
                        break;
                    }
                }
                 if(flag==0)
                {
                    a=0;break;
                }
            }


                    while(a>9)
                    {
                        if((a%10)!=0)
                        {
                            a=a%10;
                            //cout<<"a"<<a<<endl;
                        }
                        else
                        {
                            a=a/10;
                        }

                    }


        }
        if(a)
        printf("%d\n",a);
        else
        printf("Robot hanged.\n");
    }
    return 0;
}

This is the code for the following problem link.
I am getting a wrong answer despite my code working for all the test cases.
Can anyone tell me for which test case my code is not working.

A robot named as Maestro, works on right most digit of number. For eg. 123456 is a number. Maestro would work with 6. But now, if number is 12345600. It will work with 6.
Input Specification

Input a variable t which is the number of test cases. Then input a string having alternate integers and multiplication characters. Your job is to calculate the last non zero digit in that mathematical expression. Number can be as large as 10^19. 0 < t<100
Output Specification

Output consists of a single number which is the last non zero digit of the value of the expression. If the expression evaluates to be zero, output should be “Robot hanged.”
Example

Sample Input:
2
2X3X7X5
2X0X4X25


Sample Output:
1
Robot hanged.

Dani AI

Generated

The core problem in the posted code is that it operates character-by-character and multiplies digits rather than whole numeric tokens. As already pointed out, that makes inputs like 15X2 behave as 152 instead of 15*2. The original routine also uses a plain int and recomputes strlen inside the loop, which risks overflow and wasteful O(n^2) behavior when numbers can be up to 10^19.

A robust solution: split the expression on 'X' and treat each token as a full integer. If any token equals "0" the whole product is zero ("Robot hanged."). For every non-zero token, repeatedly factor out powers of 2 and 5 (count them and divide them out). Multiply the token's remaining last digit into a running result modulo 10. After all tokens, cancel paired 2/5 factors (they produce trailing zeros) and reapply leftover factors: 2^k mod 10 (cycle length 4) and any remaining 5 contributes a factor 5. Using unsigned long long (or __int128 where available) is safe for values up to 10^19; if tokens may be larger, perform the 2/5 factoring with decimal string division.

A compact C++ sketch implementing the idea:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int pow2mod10(unsigned long long e){
    if(e==0) return 1;
    int r = e % 4;
    if(r==1) return 2;
    if(r==2) return 4;
    if(r==3) return 8;
    return 6; // r==0 and e>0
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T; if(!(cin>>T)) return 0;
    while(T--){
        string expr; cin>>expr;
        unsigned long long total2=0, total5=0;
        int res=1; bool zero=false;
        size_t i=0;
        while(i<expr.size()){
            size_t j = expr.find('X', i);
            string tok = expr.substr(i, (j==string::npos ? string::npos : j-i));
            if(tok=="0"){ zero=true; break; }
            unsigned long long n=0;
            for(char c:tok) n = n*10 + (c - '0'); // safe for <=1e19
            while(n%2==0){ n/=2; ++total2; }
            while(n%5==0){ n/=5; ++total5; }
            res = (res * int(n % 10ull)) % 10;
            if(j==string::npos) break;
            i = j+1;
        }
        if(zero){ cout<<"Robot hanged.\n"; continue; }
        unsigned long long k = min(total2, total5);
        unsigned long long extra2 = total2 - k;
        unsigned long long extra5 = total5 - k;
        res = (res * pow2mod10(extra2)) % 10;
        if(extra5) res = (res * 5) % 10;
        cout<<res<<"\n";
    }
    return 0;
}

Notes: prefer unsigned long long (or __int128) to avoid overflow for up to 10^19; if token length can exceed that, replace numeric division with simple string division by 2 and 5 to remove factors. Avoid recalculating string length inside loops and verify with cases that exercise multi-digit factors, explicit zeros, and heavy 2/5 balances (the 15X2 case is a good sanity check).

There are many things that are wrong. I am not figuring all of them out for you but will give some help.

(a) Consider the test case: 15X2 Expected output 3 (because 30 goes to 3)/ Actual output 1.

So what went wrong. It is in your multiplication. You don't keep any account of multiple digit number e.g. you do 1x5x2 and get 10 which is wrong.

(b) The specification says that you will need to deal with numbers up to 1e19. That doesn't fit in an integer [on most computers].

Overall your algorithm needs a re-think. Sorry.

You have to figure out a way that you can truncate the multiplication number or ignore the parts that don't matter. That can be done (for example -- there are many ways), by noting that 10 has prime factors 2 and 5.

-- Parse a result with zero in it as special
-- Remove the factors of 10 (e.g. a 2 and a 5 prime factor). You are left with a key result:
if you have a remaining factor 5, your result is 5
if not multiply all the last digits together and keeping only the last digit at each stage

The above is definitely not optimal!! It is an example that can be shown to work with very very large input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.