Digit Extraction Algorithm & coding in MATLAB
Write a program in MATLAB to enter a number containing three digits or more. Arrange the digits of the entered numbers in ascending order and display the result.
Sample input: Enter a number 4972
Sample output: 2,4,7,9
General way to extract the digits from a number:
x=input('Enter the number');
m=x;
while(m>0)
b=rem(m,10);
% Line has to be written accoring to code
m=(m-b)/10;
end
x=input('Enter the number');
m=x;
Y=[];
while(m>0)
b=rem(m,10);
Y=[Y b];
m=(m-b)/10;
end
a=sort(Y);
Sample input: Enter a number 4972
Sample output: 2,4,7,9
x=input('Enter the number');
m=x;
while(m>0)
b=rem(m,10);
% Line has to be written accoring to code
m=(m-b)/10;
end
Explanation of the algo:
Code for this particular numerical:
x=input('Enter the number');
m=x;
Y=[];
while(m>0)
b=rem(m,10);
Y=[Y b];
m=(m-b)/10;
end
a=sort(Y);
No comments