Using MATLAB or Octave, use documenting code to Write a script that prompts the user for an integer between 0 and 9999, inclusive. The script should then calculate the digit in each of the 1000’s, 100’s, 10’s, and 1’s place of the number. Create a variable for each of the 4 extracted digits. For example, if your variables are named nThousands, nHundreds, nTens, and nOnes, then, in the case of 9471, they would be end up being set to 9, 4, 7, and 1, respectively. The script should then convert each of the 4 digits (starting with the 1000’s place digit) into an upper case letter, where 0 → A, 1 → B, 2 → C, . . . , 9 → J. Finally, the script should print the input number and the resulting 4 letters. Sample output: Enter an integer between 0 and 9999, inclusive: 9471 The characters corresponding to the digits of 9471 are J, E, H, and B Enter an integer between 0 and 9999, inclusive: 13 The characters corresponding to the digits of 13 are A, A, B, and D
Code:
clc;clear all;
A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
I=input('Enter an integer between 0 and 9999, inclusive: ');
n = floor(log10(I));
D =( mod(floor(I./ 10 .^ (n:-1:0)), 10));
N=length(D);
if N~= 4
D=[zeros(1,4-N) D];
endif
D=D+1;
fprintf('The characters corresponding to the digits of %d are %s,
%s, %s, and %s\n',I,A(D(1)),A(D(2)),A(D(3)),A(D(4)))
Output:
Enter an integer between 0 and 9999, inclusive: 13
The characters corresponding to the digits of 13 are A, A, B, and
D
Enter an integer between 0 and 9999, inclusive: 9471
The characters corresponding to the digits of 9471 are J, E, H, and
B
Get Answers For Free
Most questions answered within 1 hours.