필터 지우기
필터 지우기

how to test for palindrome of the product of unknown digit numbers

조회 수: 1 (최근 30일)
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

채택된 답변

Walter Roberson
Walter Roberson 2016년 6월 14일
for j = 10^(number_of_digits) - 1 : -1 : 10^(number_of_digits-1)
  댓글 수: 5
Walter Roberson
Walter Roberson 2016년 6월 15일
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.
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016년 6월 15일
@Walter Roberson looking closely at for j = 10^(number_of_digits) - 1 : -1 : 10^(number_of_digits-1) that you earlier suggested actually works, the problem i had earlier with it was placement of the numerous (-1).thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 6월 15일
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
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016년 6월 16일
@Walter , thanks . works now
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016년 6월 16일
@image Analyst , the goal here is to get the numbers that i will test for palindrome. the testing is up to me now . thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Analysis of Variance and Covariance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by