-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInverse.java
More file actions
42 lines (41 loc) · 1.1 KB
/
Inverse.java
File metadata and controls
42 lines (41 loc) · 1.1 KB
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
/*
Swapping the digits in an integer
with its position in the integer.
*/
import java.lang.Math;
class Inverse{
public static void checkLength(int x) {
int count = 0;
while(x>0) {
x = x%10;
count++;
}//end of while block
System.out.println(count);
}// end of checkLength
public static void digitInverse(int x) {
int value = 1;
int sum = 0;
int rem = 0;
int count = 0;
/*
Since the digits can't be greater than the length of the integer itself
*/
while (x>0){
x = x/10;
count++;
}
while (x>0) {
rem = x%10;
if (rem<(count+1)) {
sum = sum + (value * ((int) Math.pow(10, rem-1)));
x = x/10;
value++;
}// end of if condition
}// end of while loop
System.out.println(sum);
}// end of digitInverse
public static void main(String[] args) {
int x = 416523;
checkLength(x);
}
}// end of Inverse