Pattern 1; 2 3; 4 5 6; 7 8 9 10

9 views (last 30 days)
Cem Burak YALCIN
Cem Burak YALCIN on 14 May 2020
Commented: Ameer Hamza on 14 May 2020
Hello everyone, I wish you healthy days.
How can i do that pattern by using just 'for, if else or while command?
the question is that for example i input number 5, matlab giving me (4 5 6). When i input 7 matlab giving me ( 7 8 9 10)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17...
Sorry for my bad english and thanks for your help

Accepted Answer

Ameer Hamza
Ameer Hamza on 14 May 2020
Edited: Ameer Hamza on 14 May 2020
Try this. It uses the formula
n = input('Input n: ');
a = ceil((-1+sqrt(1+8*n))/2);
y = ((a-1)*a/2+1):(a*(a+1)/2);
disp(y)
  2 Comments
Cem Burak YALCIN
Cem Burak YALCIN on 14 May 2020
Thanks for your answer its work but i am looking for more basic code. By using just 'for if and while' commands.
Ameer Hamza
Ameer Hamza on 14 May 2020
If this is a homework question, then you need to show some code and ask a question about a specific issue related to MATLAB. At least, you should develop an algorithm to create these numbers.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!