#Day58 #100DaysChallenge- Matlab Loops| Finding first and last digit of the given number
#Day58-Finding first and last digit of the given number
Task:
Task:
Write a code to find first and last digit of the given number.
1234
first digit:1
last digit:4
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
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.
Matlab code
function[first,last]=firstandlastdigit(n)
first=n;
last=mod(n,10);
while(first>=10)
first=floor(first/10);
end
Sample Input and Output
>> [first,last]=firstandlastdigit(456789321)
first =
4
last =
1
>> [first,last]=firstandlastdigit(987654326)
first =
9
last =
6
Click here for Video Description
No comments