how to test for palindrome of the product of unknown digit numbers
2 views (last 30 days)
Show older comments
OLUBUKOLA ogunsola
on 14 Jun 2016
Commented: OLUBUKOLA ogunsola
on 16 Jun 2016
when testing out palindromic numbers for three digit numbers , we could create the products of the three digits this way:
for i=999:-1:100
for j=999:-1:100
answer = i*j;
what if we do not know the number of digit and the number of digit will be the input to the code , how can i adjust the 'for loop' to cater for the unknown number of digits?
say input could be 2 digits or 4 digits or 5 digits and the code will create the products
thanks
0 Comments
Accepted Answer
Walter Roberson
on 14 Jun 2016
for j = 10^(number_of_digits) - 1 : -1 : 10^(number_of_digits-1)
5 Comments
Walter Roberson
on 15 Jun 2016
I do not see anywhere in your code where your store anything into a matrix.
I tested my code from number_of_digits = 1 to number_of_digits = 4 and it works fine.
More Answers (1)
Image Analyst
on 15 Jun 2016
Try this:
% Code with a palindrome.
theNumber = 123848321; % A palindrome
str = sprintf('%d', theNumber)
if all(str == fliplr(str))
message = sprintf('%d is a palindrome', theNumber);
uiwait(helpdlg(message));
else
message = sprintf('%d is NOT a palindrome', theNumber);
uiwait(helpdlg(message));
end
% Code with a non-palindrome.
theNumber = 163848321; % NOT a palindrome
str = sprintf('%d', theNumber)
if all(str == fliplr(str))
message = sprintf('%d is a palindrome', theNumber);
uiwait(helpdlg(message));
else
message = sprintf('%d is NOT a palindrome', theNumber);
uiwait(helpdlg(message));
end
9 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!