Chewbacca and Number

Sir/maam
What is the possible error in this program:
Scanner obj = new Scanner(System.in);
long x = obj.nextLong();

	if(x<=Math.pow(10,18)){
		long original = x;
		int inc = 1;
		long rem = 0;
		while(x>0){
			rem = x%10;
			if(rem>4){
				original = original - (rem * inc);
				original = original + ((9 - rem) * inc);
			}
			x/=10;
			inc*=10;
		}
		System.out.println(original);
	}

hi @Ekram… You took long to store the integer while the input constrain is 10^100 … Long can only store number upto range of 10^19 … Please store the number in string and modify your logic

According to the given details:The first line contains a single integer x (1 ≀ x ≀ 10^18)

hi again … actually there might be few cases which can go beyond that value … so it is recommended to try it the string way … if you face a problem there you can send the code after trying …

Scanner obj = new Scanner(System.in); String x = obj.nextLine(); StringBuilder s = new StringBuilder(x); if(s.length()<=Math.pow(10,100)){ char rem = ’ '; for(int i=s.length()-1; i>=0; i–){ rem = s.charAt(i); switch (rem){ case 53: s.setCharAt(i, β€˜4’); break; case 54: s.setCharAt(i, β€˜3’); break; case 55: s.setCharAt(i, β€˜2’); break; case 56: s.setCharAt(i, β€˜1’); break; case 57: s.setCharAt(i, β€˜0’); break; } } System.out.println(s); }

Hi @Ekram,
please save the code to coding blocks ide and send the URL . I cannot read this code.

Sorry for being so late

hi again ,
See your code works fine for all digits except if there is a 9 at the end or zero at the starting …for example the output of 099999 should be 999999 but your code gives 0 . And there are some changes too as you need to use long to store your number as the input rangee is 1<x<10^18 and the limit of int is only 10^7 . so check this code and optimise it for the above test case .

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.