C++ Program to print numbers from 1 to n

Simple C++ program to print number from 1 to n .
/* C++ Program to print numbers from 1 to n */ 
#include<iostream>
using namespace std;
int main()
{
 int n,i=1;
 cout<<"Enter a number: ";
 cin>>n;
 cout<<"Number from 1 to "<<n<<" are: "<<endl;
 for(i=1;i<=n;i++)
 {
  cout<<i<<endl;
 }
 return 0;
}
/* End of the program */
Another C++ program to print numbers from 1 to n using while loop
/* C++ Program to print numbers from 1 to n */ 
#include<iostream>
using namespace std;
int main()
{
 int a,c=1;
 cout<<"Enter a number: ";
 cin>>a;
 cout<<"Numbers from 1 to "<<a<<" are: "<<endl;
 while(c<=a)
 {
  cout<<c<<endl;
  c++;
 }
 return 0;
}
/* End of the program */
Simple C++ program to print numbers from 1 to n using function
/* C++ Program to print numbers from 1 to n */ 
#include<iostream>
using namespace std;
void PrintNumbers(int n)
{
 int i;
 for(i=1;i<=n;i++)
 {
  cout<<i<<endl;
 }
}
int main()
{
 int a;
 cout<<"Enter a number: ";
 cin>>a;
 cout<<"Numbers from 1 to "<<a<<" are: "<<endl;
 PrintNumbers(a);
 return 0;
}
/* End of the program */
C++ Program to print numbers 1 to n using recursion
/* C++ Program to print numbers from 1 to n */ 
#include<iostream>
using namespace std;
void PrintNumbers(int n)
{
    if(n==0)
    {
     return ;
 }
 PrintNumbers(n-1);
 cout<<n<<endl;
}
int main()
{
 int n;
 cout<<"Enter a number: ";
 cin>>n;
 cout<<"Numbers from 1 to "<<n<<" are: "<<endl;
 PrintNumbers(n);
 return 0;
}
/* End of the program */


Popular posts from this blog