Search This Blog

#Day48#100DaysChallenge- Matlab Loops| Reverse the given number

#Day48- Reverse the given number

Task:

Print  reversed number  as shown below by taking user input for the number to be reversed.

                12345

 Reversed Number:54321

               

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those

Matlab code

function rev=reversesnum(n)

rev=0;

rem=0;

while floor(n)>0

    rem=mod(n,10);

    rev=rev*10+rem;

    n=floor(n/10);

end

Sample Input and Output

>> reversesnum(12345)

 

ans =

 

       54321

                                                       

>> reversesnum(12345)

 

ans =

 

       54321

 

>> reversesnum(87654321)

 

ans =

 

    12345678

No comments