Search This Blog

#Day58 #100DaysChallenge- Matlab Loops| Finding first and last digit of the given number

#Day58-Finding first and last digit of the given number
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


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

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();