C++ programs to print all the numbers in a range have been shown here. Both of the iterative and recursive approaches have been covered.
Page content(s):
4. Program & output (Iterative): Integer range
5. Program & output (Recursive): Integer range
1. Algorithm to Print All numbers in a range
1. Take input of two numbers n1, n2 as range limit.
2. Set another variable i=n1
3. Iterate i from n1 to n2 by incrementing 1
4. Print value of i in each iteration
2. Pseudocode to Print All numbers in a range
Input : Two integer numbers $n_1, n_2$
Output : All the integer numbers from $n_1$ to $n_2$
1. Procedure printAll($n_1$, $n_2$):
2.
3.
4.
5.
6. End Procedure
3. Time complexity to Print All numbers in a range
Time Complexity: O(n)
Here, $n$ is the no. of terms in the given range.
4. C++ Program & Output to Print All Integer numbers in a range using Iteration
/********************************************* alphabetacoder.com C++ program to print all integer numbers in a given range using iterative approach **********************************************/ #include <iostream> using namespace std; int main() { // declare variables int n1, n2, i; //take input of the ranges cout << "Enter the lower limit = "; cin >> n1; cout << "Enter the upper limit = "; cin >> n2; // iterate from n1 to n2 and print the numbers cout << "Numbers in the range: "; for (i = n1; i <= n2; i++) cout << i << " "; return 0; }
Output
Enter the lower limit = 15
Enter the upper limit = 30
Numbers in the range: 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
5. C++ Program & Output to Print All Integer numbers in a range using Recursion
/********************************************** alphabetacoder.com C++ program to print all integer numbers in a given range using a recursive approach *********************************************/ #include <iostream> using namespace std; // recursive function void printNumbers(int n1, int n2) { //condition for calling if (n2 > n1) printNumbers(n1, n2 - 1); // print current numbers cout << n2 << " "; } int main() { // declare variables int n1, n2; //take input of the ranges cout << "Enter the lower limit = "; cin >> n1; cout << "Enter the upper limit = "; cin >> n2; cout << "Numbers in the range: "; // call the recursive function to print // all the integer numbers in a range printNumbers(n1, n2); return 0; }
Output
Enter the lower limit = 10
Enter the upper limit = 15
Numbers in the range: 10 11 12 13 14 15
6. C++ Program & Output to Print All Float numbers with specific interval in a range using Iteration
/******************************************** alphabetacoder.com C++ program to print all float numbers with specific interval in a given range *********************************************/ #include <iostream> using namespace std; int main() { // declare variables double n1, n2, in, i; //take input of the ranges cout << "Enter the lower limit = "; cin >> n1; cout << "Enter the upper limit = "; cin >> n2; // take input of step cout << "Enter the value of interval = "; cin >> in; cout << "Numbers in the range: "; // dislay all numbers between n1 and n2 // with given interval for (i = n1; i <= n2; i += in) cout << i << " "; return 0; }
Output
Enter the lower limit = 10
Enter the upper limit = 25
Enter the value of interval = 0.5
Numbers in the range: 15 15.5 16 16.5 17 17.5 18 18.5 19 19.5 20 20.5 21 21.5 22 22.5 23 23.5 24 24.5 25
7. C++ Program & Output to Print All Float numbers with specific interval in a range using Recursion
/***************************************************** alphabetacoder.com C++ program to print all float numbers with specific interval in a given range using recursive approach ******************************************************/ #include <iostream> using namespace std; // recursive function void printNumbers(double n1, double n2, double in ) { //condition for calling if (n2 > n1) printNumbers(n1, n2 - in, in); // print current number cout << n2 << " "; } int main() { // declare variables double n1, n2, in; //take input of the ranges cout << "Enter the lower limit = "; cin >> n1; cout << "Enter the upper limit = "; cin >> n2; // take input of step cout << "Enter the value of interval = "; cin >> in; printf("Numbers in the range: "); // call the recursive function to print // all the float numbers in input range printNumbers(n1, n2, in); return 0; }
Output
Enter the lower limit = 100
Enter the upper limit = 101
Enter the value of interval = 0.07
Numbers in the range: 99.95 100.02 100.09 100.16 100.23 100.3 100.37 100.44 100.51 100.58 100.65 100.72 100.79 100.86 100.93 101