for statement program

what is the for statement to ouuput the numbers 1 to 20 ,each on anew line;

Last edited on
Do you know how to use a for loop?
Do you know how to print numbers and new lines?
the output like this
1
2
3
4
to 20


i do not know how to print the numbers and new line in for loop






Last edited on
please help for solve this program :(
i do not know how to print the numbers and new line in for loop
It's the same way to output numbers outside for loop.
1
2
3
4
int main ()
{
  for (int n20; n>0; n--) {
    cout << n << ", ";


its ok;or not;
Last edited on
Why not just google "for loop in c++"? Then google "new line C++".
Last edited on
iota_n( std::ostream_iterator<int>( std::cout, "\n" ), 20, 1 );

About iota_n you can read here http://cpp.forum24.ru/?1-3-0-00000046-000-0-0-1343496167 by translating the text from Russian in English by means of google translator.
thxs

the solution :)
'
1
2
3
4
int i; 
{
for(int=1; i<=20; i++){
cout << "i"<< endl; 
Last edited on
A loop-free solution for variety:

1
2
3
4
5
6
7
8
#include <iostream>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm.hpp>
int main()
{
    copy( boost::irange<int>(1, 21),
          std::ostream_iterator<int>(std::cout, "\n"));
}

online demo: http://liveworkspace.org/code/967399fd6d938bfd3d6500a76b5ab3cf
Last edited on
@nic23 your solution is wrong, it is supposed to be:

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main() {
	for(int i=0; i<21; i++) {
		cout<<i<<endl;
	}
	return(0);
}
Topic archived. No new replies allowed.