Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*** If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5

*** If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. Write a function called number2letters that returns the number of letters needed to write down the number n in words. For example, 342 (three hundred forty two) contains 20 letters. Notice that we do not count spaces, nor do we use hyphens. The only input to the function is n, a positive integer smaller than 1000, but you do not need to check this. (Inspired by Project Euler.)

my code is

function words = num2letters(number) % calculating integer part number int_number = floor(number); words = int2words(int_number); if int_number ~= number %% intital words digitNames = {'zero', 'one', 'two', 'three', 'four', 'five', ... 'six', 'seven', 'eight', 'nine'}; words = [words, ' point']; numStr = sprintf('%.18g', number); pointPos = find(numStr == '.'); numStr = numStr(pointPos+1:end); for index = 1:length(numStr) int_number = str2num(numStr(index)); words = [words, ' ', digitNames{int_number+1}]; end end end

% making according to british scale function words = int2words(number) %% base words minus = 'minus'; names0 = 'zero'; words1000 = {'', ' thousand', ' million', ' billion', ' trillion', ... ' quadrillion', ' quintillion', ' sextillion', ... ' septillion', ' octillion', ' nonillion', ... ' decillion', ' undecillion', ' duodecillion', ... ' tredecillion', ' quattuordecillion', ... ' quindecillion', ' sexdecillion', ' septendecillion', ... ' ocotdecillion', ' novemdecillion', ' vigintillion'};

% calculating sign if number < 0 isNegative = true; number = -number; elseif number == 0 words = names0; return; else isNegative = false; end words = '';

% conversion starts here cnt = 1; while number ~= 0 num1000 = mod(number,1000); number = floor(number / 1000); if num1000 > 0 if ~isempty(words) words = [',' words]; %#ok end words = [' ' int2words_1to999(num1000), words1000{cnt}, words]; %#ok end cnt = cnt + 1; % check: if cnt == length(words1000) + 1 error(' number is too large'); end end

%% adding sign if isNegative words = [minus, words]; end words = strtrim(words); end

% numbers between 1 and 999 function words = int2words_1to999(number) %% base words names1 = {'', 'one', 'two', 'three', 'four', 'five', 'six', ... 'seven', 'eight', 'nine', 'ten', 'eleven', ... 'twelve', 'thirteen', 'fourteen', 'fifteen', ... 'sixteen', 'seventeen', 'eighteen', 'nineteen'}; names10 = {'', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', ... 'seventy', 'eighty', 'ninety'}; %% convert num100 = mod(number, 100); if num100 < 20 words = names1{num100+1}; number = (number - num100) / 100; else num10 = mod(number, 10); words = names1{num10+1}; if ~isempty(words) words = ['-' words]; end number = (number - num10) / 10; num10 = mod(number, 10); words = [names10{num10} words]; number = (number - num10) / 10; end if number > 0 if ~isempty(words) words = [' ', words]; end words = [names1{number+1}, ' hundred', words]; end end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions