-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrotate_number.cpp
More file actions
55 lines (39 loc) · 1.18 KB
/
rotate_number.cpp
File metadata and controls
55 lines (39 loc) · 1.18 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
43
44
45
46
47
48
49
50
51
52
53
54
55
// https://youtu.be/ysMHtDIlU0c?list=PL-Jc9J83PIiFj7YSPl2ulcpwy-mwj1SSk
/*
1. You are given two numbers n and k. You are required to rotate n, k times to the right.
If k is positive, rotate to the right i.e. remove rightmost digit and make it leftmost.
Do the reverse for negative value of k. Also k can have an absolute value larger than number of digits in n.
2. Take as input n and k.
3. Print the rotated number.
4. Note - Assume that the number of rotations will not cause leading 0's in the result. e.g. such an input will not be given
n = 12340056
k = 3
r = 05612340
ex: n = 273516 , k=2 , ans = 162735
*/
//Solution : https://youtu.be/lt8oCGqYMGg?list=PL-Jc9J83PIiFj7YSPl2ulcpwy-mwj1SSk
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
int cn=n;//copy of number
int count_digits=0;
while(n>0)
{
count_digits++;
n/=10;
}
k=k%count_digits;
if(k<0)
k+=count_digits;
int divisor=pow(10,k);
int multiplier=pow(10,count_digits-k);
int quotient=cn/divisor;
int rem=cn%divisor;
int first_part=multiplier*rem;
int ans=first_part+quotient;
cout<<ans<<"\n";
}