Patrick and Johnny started to play the next card game. In this game, the instructor gave Patrick a card with an integer number x and the game is to invert digits in it. Inverting digit t means replacing it with digit 9 - t. Patrick has to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero.
Can you write a C program to transform the number to the minimum possible positive number using while loop?
Input Format:
The input consists of an integer 'x', which corresponds to the number that instructor gave to Patrick.
Output Format:
Print the minimum possible positive number that Patrick can obtain after inverting some digits. The number shouldn't contain leading zeroes.
[All text in bold corresponds to the input and the rest corresponds to output]
Sample Input and Output 1:
Enter the number
22
The minimum possible positive number is 22
Sample Input and Output 2:
Enter the number
8772
The minimum possible positive number is 1222
Program Code:
#include <string.h>
#include <stdio.h>
void change(char *x)
{
*x = '0' + 9 - *x + '0';
}
int main()
{
char num[64];
printf("Enter the number\n");
scanf("%s", num);
if(num[0]!='9' && num[0]>='5')
change(&num[0]);
int i=1;
while(i < strlen(num))
{
if(num[i]>='5')
change(&num[i]);
++i;
}
printf("The minimum possible positive number is ");
puts(num);
return 0;
}
Post A Comment:
0 comments: