Write a program that will ask the user to enter how many numbers he/she wants to
ID: 669941 • Letter: W
Question
Write a program that will ask the user to enter how many numbers he/she wants to enter into an array, then input those numbers (all via keyboard) and store them into an array. Finally, in that same program, please find the product of all elements in that array and display the following sentence: “The product of all the numbers you entered is <what ever was by your program>.”
I got some of it but can't figure out how to create an array to display the numbers entered in the loop.
%Ask to input number for array
n = input('How many numbers would you like in the array? ');
%Loop
for i = 1:n
number = input('Enter number for array ');
end
%Calculate sum of array
disp(1:n)
product = prod(1:n);
disp(['The product of all the numbers you entered is ' num2str(product)])
Explanation / Answer
prompt='How many numbers would you like in the array ';
n=input(prompt);
array(1,n)=0;
sum=0;
prod=1;
for i=1:n
if i==1
array(1,i)=input('first number ');
else
if i==n
array(1,i)=input('last number ');
else
array(1,i)=input('next number ');
end
end
sum=sum+array(1,i);
end
for i=1:n
prod=prod*array(1,i);
end
fprintf('The product of all numbers in array is %d ',prod);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.