C++ program to print the natural numbers using for loop
kw.cpp
#include<iostream>usingnamespacestd;intmain(){intn,i;cout<<"How many numbers you want to print ";cin>>n;for(i=1;i<=n;i++)cout<<i<<" ";cout<<"\n";return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
How many numbers you want to print 10
1 2 3 4 5 6 7 8 9 10
kodingwindow@kw:~$
C++ program to print the natural numbers using while loop
kw.cpp
#include<iostream>usingnamespacestd;intmain(){intn,i;cout<<"How many numbers you want to print ";cin>>n;i=1;while(i<=n){cout<<i<<" ";i++;}cout<<"\n";return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
How many numbers you want to print 10
1 2 3 4 5 6 7 8 9 10
kodingwindow@kw:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.