-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate-a-number.java
38 lines (30 loc) · 921 Bytes
/
rotate-a-number.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
import java.util.*;
public class Main {
// Function to count the digits in a number
static int digitCount(int num){
int count = 0;
while (num != 0) {
num /= 10;
count ++;
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
int k= sc.nextInt();
// finding no of digits in a number
int digits = digitCount(n);
// for k >= digits of a number
k = k % digits;
// for -ve value of k
if(k<0){
k += digits;
}
// finding rotated value by taking +ve k in consideration first
int divisor = (int) Math.pow(10, k);
int rotValue = (n % divisor) * (int)Math.pow(10, digits-k)
+ (n / divisor);
System.out.println(rotValue);
}
}