How do I print a sentence "Hello world!" n number of times?
286 views (last 30 days)
Show older comments
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
Accepted Answer
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.
0 Comments
More Answers (0)
See Also
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!