-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReplaceNum.java
59 lines (54 loc) · 1.35 KB
/
ReplaceNum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Substract any number between 0 to 1000000 each digit by 9
* Input : 25843
* Output : 74156
*
* Input : 58462314
* Output : Given integer is out of range
*/
import java.util.*;
public class ReplaceNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int rem, val=0;
if(n>=0 && n<=1000000)
{
int m=0;
while(n>0)
{
rem = n%10;
val = (int)((9-rem)*Math.pow(10,m)+val);
n = n/10;
m++;
}
System.out.println(val);
}
else
{
System.out.println("Given integer is out of range");
}
}
}
/* ANOTHER SOLUTION
* ---------------------------
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String num = sc.nextLine();
int val = 0;
if(Integer.parseInt(num)>=0 && Integer.parseInt(num)<=1000000)
{
for (int i = 0; i < num.length(); i++)
{
int k = 9-Integer.parseInt(String.valueOf(num.charAt(i)));
val = val*10 + k;
}
System.out.println(val);
}
else
{
}
}
}
*/