Problem with disp code with variables
Show older comments
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 Comments
Steven Latham
on 11 Dec 2019
Edited: Steven Latham
on 11 Dec 2019
Steven Latham
on 11 Dec 2019
Answers (1)
Maadhav Akula
on 13 Dec 2019
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!
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!