Flipped numbers (number pyramid)

12 vues (au cours des 30 derniers jours)
Mikaila Denise Loanzon
Mikaila Denise Loanzon le 31 Mar 2020
Commenté : Birdman le 31 Mar 2020
% Please help on how can I do a flipped pyramid of integers from 1-9.
% I have this code
n = 4;
for k = 1:n;
for c = 1:n-k;
fprintf(' ');
end
for c = 1:n
fprintf('%d', 1:k);
end
fprintf('\n');
end
However, this code outputs:
1111
12121212
123123123123
1234123412341234
An integer must be inputted and then this number must also correspond to the number of rows that is printed in the command window, along with the increasing numbers up to the inputted number. For example, if I input 4, the output should be this.
1
12
123
1234
Thank you!
  1 commentaire
Birdman
Birdman le 31 Mar 2020
Check my code.

Connectez-vous pour commenter.

Réponses (1)

Birdman
Birdman le 31 Mar 2020
n=4;
for i=1:n
A(i)=str2double(string(sprintf('%d',1:i)));
end
disp(A)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by