Search This Blog

#Day55 #100DaysChallenge- Matlab Loops|Swapping Numbers in loop

#Day55-Swapping Numbers in loop

Task:

Write a code to swap a number in loop
swap(13,2,7,3)

Swap 1:2,7,13
Swap2:7,13,2
Swap3:13,2,7

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

Matlab code

function  swapelements_loop(x,y,z,swap)
while swap>0
temp=0;
temp=x;
x=y;
y=z;
z=temp;
swap=swap-1;
fprintf('New X Y Z VALUE %d %d %d Swap No: %d',x,y,z,swap+1);
fprintf('\n');
end

 end

Sample Input and Output
swapelements_loop(5,6,7,3)
New X Y Z VALUE 6 7 5 Swap No: 3
New X Y Z VALUE 7 5 6 Swap No: 2
New X Y Z VALUE 5 6 7 Swap No: 1

Click Here for Video Description




Live Support: t.me/matlabirawen


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); })();