to print following pattern

8 ビュー (過去 30 日間)
Arpit Jain
Arpit Jain 2019 年 5 月 22 日
コメント済み: KALYAN ACHARJYA 2019 年 5 月 22 日
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
  1 件のコメント
Alex Mcaulley
Alex Mcaulley 2019 年 5 月 22 日
What have you tried so far?

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

採用された回答

Rik
Rik 2019 年 5 月 22 日
You mean like this?
for k=1:9
a=str2double(char('0'+(1:k)));
b=a*8+k;
fprintf('%d x 8 + %d = %d\n',a,k,b)
end

その他の回答 (2 件)

Stephen23
Stephen23 2019 年 5 月 22 日
編集済み: Stephen23 2019 年 5 月 22 日
Three lines, no loop, and only basic numeric operations used:
>> X = 1:9;
>> Y = floor((137174210/1111111111)*10.^X);
>> fprintf('%d x 8 + %d = %d\n',[Y;X;Y.*8+X])
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
  1 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 22 日
Auu.. Matlab Master ..Without Loop..Great @Stephan +1

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


KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 22 日
編集済み: KALYAN ACHARJYA 2019 年 5 月 22 日
Today I lerned this polyval function, thats why I have posted 2nd answer, although the correct answer already given by @Rik
for i=1:9
value=polyval((1:i),10);
result=value*8+i;
fprintf('\n %d x 8 + %d = %d',value,i,result);
end
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
  2 件のコメント
Rik
Rik 2019 年 5 月 22 日
That's nice lateral thinking with polyval. Note that our answers return different answers after 9:
%my answer:
NaN x 8 + 10 = NaN
%polyval:
1234567900 x 8 + 10 = 9876543210
That is caused by my trick with char. If you avoid that, you can also generate this:
12345678910 x 8 + 10 = 98765431290
%replace this:
a=str2double(char('0'+(1:k)));
%with this:
c=cell2mat(cellfun(@num2str,num2cell(1:k),'Uni',0));
a=str2double(c);
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 22 日
Yes @Rik Thanks

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by