フィルターのクリア

Problem with disp code with variables

12 ビュー (過去 30 日間)
Steven Latham
Steven Latham 2019 年 12 月 11 日
回答済み: Maadhav Akula 2019 年 12 月 13 日
Hey round 2 for me. Working on a homework problem for a simple code for some reason it's not working and I'm not getting enough details to fix the issue the code is below. my problem is the if else lines.
clc, clear
prompt = 'Enter a WHOLE number ';
x = input(prompt);
y = sqrt(x);
if isreal(y)
disp('The number ',x,' is a square; it is the square of the number ',y)
else
disp(' The number ',x,' is not a square.')
end
  2 件のコメント
Steven Latham
Steven Latham 2019 年 12 月 11 日
編集済み: Steven Latham 2019 年 12 月 11 日
okay I figured out the first part of my problem I just needed to add brackets [ ] to each statement. new code below.
clc, clear
prompt = 'Enter a positive integer ';
x = input(prompt);
y = sqrt(x);
if isreal(y)
disp(['The number ',x,' is a square; it is the square of the number ',(y)])
else
disp([' The number ',(x),' is not a square.'])
end
Now i just need to get it to actually display the variables instead of boxes
Enter a positive integer 25
The number is a square; it is the square of the number
Steven Latham
Steven Latham 2019 年 12 月 11 日
nope back to broken the else side of the function doesn't seem to work if i take the sqrt of 122 it still pops up in the first box rather than moving to the second.

サインインしてコメントする。

回答 (1 件)

Maadhav Akula
Maadhav Akula 2019 年 12 月 13 日
If you are checking for whether a given number is a perfect square or not, then the following changes to your code might help:
clc, clear
prompt = 'Enter a WHOLE number ';
x = input(prompt);
y = sqrt(x);
if mod(y,1)==0
disp(['The number ',num2str(x),' is a square; it is the square of the number ',num2str(y)])
else
disp([' The number ',num2str(x),' is not a square.'])
end
Hope this helps!

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by