Write a C++ Program to Print N natural numbers from 1 to given value. This C++ program allows you to enter the maximum number to print natural numbers. Next, we used the for loop to iterate from 1 to that number by incrementing the i value. Within the loop, we print the i value.
#include<iostream> using namespace std; int main() { int number; cout << "\nPlease Enter Integer Value to print Natural Numbers = "; cin >> number; cout << "\nList of Natural Numbers from 1 to " << number << " are\n"; for(int i = 1; i <= number; i++) { cout << i <<" "; } return 0; }
Please Enter Integer Value to print Natural Numbers = 15
List of Natural Numbers from 1 to 15 are
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
C++ program to Print N Natural Numbers using While Loop
#include<iostream> using namespace std; int main() { int number, i = 1; cout << "\nPlease Enter Integer Value to print Natural Numbers = "; cin >> number; cout << "\nList of Natural Numbers from 1 to " << number << " are\n"; while(i <= number) { cout << i <<" "; i++; } return 0; }
Please Enter Integer Value to print Natural Numbers = 25
List of Natural Numbers from 1 to 25 are
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
This C++ print natural numbers example allows entering a minimum and maximum value. Next, it prints natural numbers between minimum and maximum.
#include<iostream> using namespace std; int main() { int start, end; cout << "\nPlease Enter the Start Value to print Natural Numbers = "; cin >> start; cout << "\nPlease Enter the End Value to print Natural Numbers = "; cin >> end; cout << "\nList of Natural Numbers from " << start << " to " << end << " are\n"; for(int i = start; i <= end; i++) { cout << i <<" "; } return 0; }