Breaking a Number into 1000s, 100, 50, 10, 5,1

3 views (last 30 days)
Hello Everyone. I am trying to figure out a way to automatically break a given number into sums of 1000, 500, 100, 50, 10, 5 and 1. For example given 1994, I want to break it into 1000 + 500 + 100 + 100 + 100 + 100 +50+ 10 + 10 + 10 +10 + 4. Or given 75 it would be 50 + 10 + 10 + 5. I would appreciate any insight on how to do this. I am trying to use this to create a function that converts a given value into Roman numerals.
  2 Comments
DGM
DGM on 25 May 2021
There are a number of user-submitted tools on the File Exchange that can do conversion between various number systems. You could look at a few and see what approaches the various authors use.

Sign in to comment.

Accepted Answer

Ntandoyakhe Tshuma
Ntandoyakhe Tshuma on 25 May 2021
**I figured it out, my solution was big in size but it worked.

More Answers (1)

Jan
Jan on 25 May 2021
This sounds like a homework question. So please show us, what you have tried so far and ask a specific question concerning Matlab.
n = 1994;
pool = [1000, 500, 100, 50, 10, 5, 1];
v = zeros(size(pool));
for k = 1:numel(pool)
v(k) = ? % How many multiples of pool(k) are matching into n?
n = ? % Reduce n by number of found multiples
end
  1 Comment
Ntandoyakhe Tshuma
Ntandoyakhe Tshuma on 25 May 2021
Edited: Rik on 25 May 2021
I am working on a matlab cody problem. I have linked it here below. I am trying to create a function that converts a given number into roman numerals. I first created a function that converts roman numerals to digit form (see sample of my code below) . What l am working on is the reverse of that. It is not a homework problem l am just trying to advance my matlab skill by practicising.
I=1;
V=5;
X=10;
L=50;
C=100;
D=500;
M=1000;
%% make an array rnum
N=4;
rnum=zeros(1,N);
%% Initialize sumations
sum1=0;
sum2=0;
%% Ask the user to input their roman numeral
for ix=1:N
rnum(ix)=input(['Enter value ',num2str(ix),' of your roman numeral: '],'s');
if rnum(ix)=='I'
rnum(ix)=1;
elseif rnum(ix)=='V'
rnum(ix)=5;
elseif rnum(ix)=='X'
rnum(ix)=10;
elseif rnum(ix)=='L'
rnum(ix)=50;
elseif rnum(ix)=='C'
rnum(ix)=100;
elseif rnum(ix)=='D'
rnum(ix)=500;
elseif rnum(ix)=='M'
rnum(ix)=1000;
else
disp('Please Follow The Guide')
break
end
end
disp(num2str(rnum))
%% Compute the morden numerical value
if rnum(1)>rnum(2)
sum1=rnum(1)+rnum(2);
elseif rnum(1)==rnum(2)
sum1=rnum(1)+rnum(2);
elseif rnum(1)<rnum(2)
sum1=rnum(2)-rnum(1);
end
if rnum(3)>rnum(4)
sum2=rnum(3)+rnum(4);
elseif rnum(3)==rnum(4)
sum1=rnum(1)+rnum(2);
elseif rnum(3)<rnum(4)
sum2=rnum(4)-rnum(3);
end
disp(['Your value in morden numbers is',num2str(sum1+sum2)])

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!