-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRotateNumber.java
More file actions
38 lines (32 loc) · 946 Bytes
/
RotateNumber.java
File metadata and controls
38 lines (32 loc) · 946 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
package codes;
import java.util.Scanner;
public class RotateNumber {
// n is a postive n>1 and n<10000
// k is negative
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Number");
int n = scanner.nextInt();
System.out.println("Enter the No of Rotations");
int k = scanner.nextInt();
// 1st Step - Count Digit
int countDigit = 0;
int n2 = n; // Copy of n because n will destroy after the loop
while(n!=0) {
countDigit++;
n = n/10;
}
// If k is Negative
if(k<0) {
k = k + countDigit;
}
// Assume if it is large than countOf Digit
k = k % countDigit; // Now it settle down in number of digit range
int leftPart = n2 / (int)Math.pow(10,k);
int rightPart = n2 % (int) Math.pow(10, k);
int power = countDigit - k;
int result = rightPart * (int)Math.pow(10, power) + leftPart;
System.out.println(result);
scanner.close();
}
}