Header Ads Widget

Write a program to print first N natural numbers in C++. (N is given by user)

 

Write a program to print first N natural numbers in C++. (N is given by user) 

Given a number n , write a program to print the first n natural numbers . Value of n is given by user .

Sample input:

10

Sample output:

 List of Natural Numbers from 1 to 10 are 
 1  2  3  4  5  6  7  8  9  10

Write a program to print first N natural numbers in C++. (N is given by user) 

The objective of the code is to print the first n natural number . n is given by the user .

Code(C++):-

#include <iostream>
using namespace std;
int main()
{
int n, i;
cout<<"\nPlease Enter any Integer Value : ";
cin>>n;
cout<<"\nList of Natural Numbers from 1 to "<<n<<" are \n";
for(i=1;i<=n;i++)
{
cout<<i<<" ";
}
return(0);
}

Output:-
Please Enter any Integer Value :
10
List of Natural Numbers from 1 to 10 are
1 2 3 4 5 6 7 8 9 10


Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-