How do I print out a product of 2 requested numbers
조회 수: 1 (최근 30일)
이전 댓글 표시
I need to write a function that requests the user to input 2 numbers one by one and then prints them and their prodct according to:
(I'm using the example numbers it gives me)
>> print_product Give me a number: 4 Give me another number: 7 The product of 4 and 7 is 28.
function [] = print_product ()
clc
x = input('Give me a number: ', 's');
y = input('Give me another number: ', 's');
z = x*y;
fprintf ('The product of x and y is z ')
댓글 수: 0
채택된 답변
Stephen23
2018년 2월 28일
편집: Stephen23
2018년 2월 28일
function print_product()
x = str2double(input('Give me a number: ', 's'));
y = str2double(input('Give me another number: ', 's'));
z = x*y
fprintf('The product of %d and %d is %d',x,y,z)
end
댓글 수: 2
Stephen23
2018년 2월 28일
input('...','s') returns a character vector (a string), and str2double converts that into a numeric (which you need for doing any numeric operations, e.g. times).
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!