How do I print out a product of 2 requested numbers

1 vue (au cours des 30 derniers jours)
Matthew Orsie
Matthew Orsie le 28 Fév 2018
Commenté : Stephen23 le 28 Fév 2018
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 ')

Réponse acceptée

Stephen23
Stephen23 le 28 Fév 2018
Modifié(e) : Stephen23 le 28 Fév 2018
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 commentaires
Matthew Orsie
Matthew Orsie le 28 Fév 2018
What does that str2double function do? The explanations online don't really help
Stephen23
Stephen23 le 28 Fév 2018
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).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by