Chewbacca And Numbers

Solution  :-

Using long long int array :-

#include<iostream>
using namespace std;

int main() {
	long long int n;
	cin >> n;
	long long int arr[18],i=0,j;
	while (n != 0) {
		arr[i] = n % 10;
		n = n / 10;
		i++;
	}
	for (j = 0;j < i;j++) {
		if (arr[j] >= 5) {
			arr[j] = 9-arr[j];
		}
	}
	long long int k=1;
	for (j = 0;j < i;j++) {
		n = n+arr[j]*k;
		if (j == i-1&&n/k==0) {
			n = 9*k+n%k;
		}
		k = k * 10;
	}
	cout << n << endl;
	return 0;
}

 

Using char array  :-

#include<iostream>
using namespace std;

int main() {
	char a[20];
	cin >> a;
	int i = 0;
	if (a[0] == '9') {
		i++;
	}
	for (;a[i] != '';i++) {
		int digit = a[i] - '0';
		if (digit >= 5) {
			digit = 9 - digit;
			a[i] = digit + '0';
		}
	}
	cout << a << endl;
	return 0;
}

Contact us





JDoodle for WordPress
JDoodle for WordPress
Scroll Up