July 17, 2017 Andrey

Finding a next lucky number with C++.

This is an easy task for Monday from Week of Code at Hackerrank.

The task is to find a next lucky number based on given 6 digit integer.

#include <bits/stdc++.h>
#include <string> 

using namespace std;

bool isLucky(int x) {
    
    int digit6 = x % 10;
    int digit5 = (int)((x % 100) / 10);
    int digit4 = (int)((x % 1000) / 100);
    int digit3 = (int)((x % 10000) / 1000);
    int digit2 = (int)((x % 100000) / 10000);
    int digit1 = (int)(x / 100000);
    
    if ((digit1 + digit2 + digit3) == (digit4 + digit5 + digit6))
        return true;
    else 
        return false;

}

string onceInATram(int x) {
    int i = x+1;
    
    while(true){
        if (isLucky(i))
            break;
        else 
            i++;
    }
   
    return to_string(i);
}

int main() {
    int x;
    cin >> x;
    string result = onceInATram(x);
    cout << result << endl;
    return 0;
}