fprintf and \n (pyramid of numbers)

조회 수: 4 (최근 30일)
yonatan friedman
yonatan friedman 2019년 12월 31일
답변: Maadhav Akula 2020년 1월 3일
clc; clear;
n = 6;
j = [];
for i = 1 : n
for j = i:n
fprintf('%d',i:j)
fprintf('\n')
end
end
~~~~~~~~~~~~~~~~~~
i want to do pyramid of numbers
for example n = 6
the output is :
1
23
456
  댓글 수: 1
dpb
dpb 2019년 12월 31일
Use fixed-width fields...

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

답변 (1개)

Maadhav Akula
Maadhav Akula 2020년 1월 3일
The following code might be helpful:
clc; clear;
n = 3;
i = 1;
j = 1;
l = 1;
while l<=n
while j<=i && l<=n
fprintf('%d',l)
l = l + 1;
j = j + 1;
end
fprintf('\n')
i = i + 1;
j = 1;
end
Hope this helps!

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by