-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.)rotate_a_number_by_k.java
More file actions
44 lines (37 loc) · 998 Bytes
/
7.)rotate_a_number_by_k.java
File metadata and controls
44 lines (37 loc) · 998 Bytes
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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// Input lena
int n = scn.nextInt(); // input number dalo
int k = scn.nextInt(); // rotation kitni baar
// Step 1: Count digits
int temp = n;
int nod = 0;
while (temp > 0) {
temp = temp / 10;
nod++;
}
// Step 2: Normalize k
k = k % nod;
if (k < 0) {
k = k + nod;
}
// Step 3: Find divisor and multiplier
int div = 1;
int mult = 1;
for (int i = 1; i <= nod; i++) {
if (i <= k) {
div = div * 10;
} else {
mult = mult * 10;
}
}
// Step 4: Split number
int q = n / div;
int r = n % div;
// Step 5: Rotate and print
int ans = r * mult + q;
System.out.println(ans);
}
}