Chewbacca and number

Test Case 0,1,2 is passed.3,4 is not…First tell me whats wrong in my code and then rectify it pls

#include
using namespace std;
int invert(int n){
int p=0;
int a=1;
while(n!=0){

	int ld=n%10;
	
	if(n==9){
		p=p+(ld*a);
		break;
	}
	int newn=9-ld;
	if(newn<ld){
		p=p+(newn*a);
	}
	else{
		p=p+(ld*a);
	}
	n=n/10;
	a=a*10;
}
return p;

}
int main() {
int n;
cin>>n;
cout<<invert(n)<<endl;
return 0;
}

@rabimajumder08 your logic is correct you are just missing out large numbers for example if n is more than 12 digits long then it will go out of bounds as you have taken int everywhere.
this is the reason your submission is giving WA.you just have to add 1 line #define int long long; and modify int main() to int32_t main().

corrected code is

#include
using namespace std;
#define int long long
int invert(int n){
int p=0;
int a=1;
while(n>0){

	int ld=n%10;
	n=n/10;
	int newn=9-ld;
	if(newn<ld){
		p=p+(newn*a);
	}
	else{
		p=p+(ld*a);
	}
	a=a*10;
}
return p;

}
int32_t main() {
int n;
cin>>n;
cout<<invert(n)<<endl;
return 0;
}

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.