Use fprintf to create a multiplication table.

8 ビュー (過去 30 日間)
Jeff Cox
Jeff Cox 2011 年 3 月 20 日
回答済み: van exel vispo 2020 年 4 月 20 日
Create a multiplication table from 1 to 13 for the number 6. Your table should look like this.
1 times 6 is 6 2 times 6 is 12 3 times 6 is 18 . . .

採用された回答

Paulo Silva
Paulo Silva 2011 年 3 月 20 日
clc
Start=6;
Stop=6;
for u=Start:Stop;
for v=1:13;
fprintf('%d times %d is %d\n',u,v,u*v)
end
fprintf('\n')
end
The code works for more than what is requested, for example create several multiplication tables
clc
Start=1;
Stop=10;
for u=Start:Stop;
for v=1:10;
fprintf('%d times %d is %d\n',u,v,u*v)
end
fprintf('\n')
end
Simple version for just what is requested
clc
for v=1:13
fprintf('%d times %d is %d\n',6,v,6*v)
end
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 3 月 21 日
Paulo, as this was obviously a homework question, it is a good thing that you left in a deliberate bug.

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

その他の回答 (4 件)

Mohamed Bireir
Mohamed Bireir 2019 年 9 月 16 日
clc
v=1:1:13;
u=6.*v;
table=[v;u]
fprintf('%d times %d is %d\n',6,v,6*v)
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 9 月 16 日
6 times 1 is 2
3 times 4 is 5
6 times 7 is 8
9 times 10 is 11
12 times 13 is 6
12 times 18 is 24
30 times 36 is 42
48 times 54 is 60
66 times 72 is 78
I don't think that is the required output...

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


van exel vispo
van exel vispo 2020 年 4 月 20 日
This should work better
n = 1:13;
m = n.*6;
table = [n;m]
fprintf('%d times 6 is %d \n',table)

Walter Roberson
Walter Roberson 2011 年 3 月 20 日
See the examples in the fprintf documentation.

Alexander Pollock
Alexander Pollock 2019 年 3 月 3 日
How to do this without using loops ? for example if and else statements ?
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 3 月 9 日
Suppose you want to print a table with three columns of values, with the value stored in c1, c2, c3. Without assuming that c1, c2, c3 are currently stored as columns, you can construct
C = [c1(:), c2(:), c3(:)];
and now C is a something by 3 array of values to be output.
Now here comes the trick:
fprintf('text for first goes here %f text for second goes here %f text for third goes here %f\n', C.' ); %notice the transpose

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by