Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

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

Popular Posts