How do I print a sentence "Hello world!" n number of times?

286 views (last 30 days)
n is the value you would enter to display it that number of times, for example:
n = 3
the answer would be
1) Hello world!
2) Hello world!
3) Hello world!
Thanks
  3 Comments
Star Strider
Star Strider on 29 Apr 2015
You already mentioned the functions you need in your tags.
patty
patty on 29 Apr 2015
Edited: James Tursa on 29 Apr 2015
I tried this
n = input('Enter a number:');
for sentence = n:1
n = fprintf('%d. Hello world!', n);
disp(n:1)
end
I'm new to matlab so I was hoping someone could help me figure it out. thanks

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 29 Apr 2015
Edited: James Tursa on 29 Apr 2015
Since you posted your code I will make some corrections for you:
n = input('Enter a number:');
for sentence = 1:n % have the indexing go from 1 to n, not reverse
fprintf('%d. Hello world!\n', sentence); % print the index, and a newline \n
end
The fprintf will print out your line. You don't need the extra disp.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!