mardi 19 janvier 2021

how can i print reverse of a big 10 digit number by using arrays?

I've been trying to code a function to print the reveres version of a utmost 10 digit number by using arrays...but the problem here is that my code can't print the reverse of a big 10 digit number (like 9999999999 or 9784562312) correctly... I can't find out where the problem is!! Anyone have an opinion? (sorry if I had Grammatical error, I'm not a native speaker)

and here is the code:

int main(){

    const int size = 10;    
    int number, revers;
    
    cin >> number;
    revers = inverse( number, size );
    cout<<revers;
}


int inverse( int n, int size){

    int sum=0, array[size]={0}, i=0;
        
        for( i=0 ; n>=1 ; i++ ){
            
            array[i] = n % 10;
            n /= 10;
        }
    
    int l=i;
        i--;
    
    for(int j=0 ; j<l ; j++, i--){
        sum += pow(10,i)*array[j];
    }
    
    return sum;
}

Aucun commentaire:

Enregistrer un commentaire