3 Print and Submit Create a script that asks the user for ho

3. Print and Submit Create a script that asks the user for how many bits they have in a binary word. Return the number of possible values a word of that length can have as a displayed number in the command line. Comment your code.

Solution

Matlab code:

n = input(\'Please Enter the number of bits in the word\ \'); %take input
%number of values that word can have = 2^n as there are 2^n different binary numbers with length n
if(n <= 0)
fprintf(\'Number of bits must be positive\ \')
else
fprintf(\'Number of possible values this word can have is %i\ \', 2^n);
end

Sample output:

1)

Please Enter the number of bits in the word
5
Number of possible values this word can have is 32
2)
Please Enter the number of bits in the word
-2
Number of bits must be positive

 3. Print and Submit Create a script that asks the user for how many bits they have in a binary word. Return the number of possible values a word of that length

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site