#Day59 #100DaysChallenge- Matlab Loops| Swapping first and last digit of the given number
#Day59-Swapping first and last digit of the given number
Task:
Task:
Write a code to swap first and last digit of the given number.
1234
first digit:1
last digit:4
Swapped Number:4231
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.
Matlab code
1234
first digit:1
last digit:4
Swapped Number:4231
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.
Matlab code
function swappednum=swapfirstnlastdigits(n)
[first,last]=firstandlastdigit(n)%calling the function.
digits=floor(log10(n));
swappednum=(((last*10^digits)+(floor(mod(n,10^digits)))-last)+first);
end
Sample Input and Output
>> swappednum=swapfirstnlastdigits(67541)
first =
6
last =
1
swappednum =
17546
>> swappednum=swapfirstnlastdigits(12345)
first =
1
last =
5
swappednum =
52341
>> swappednum=swapfirstnlastdigits(999834567)
first =
9
last =
7
swappednum =
799834569
Click here for Video description
Free Codes: youtube.com/castorclasses
No comments