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 <stdio.h> int main() { // declare variables int n1, n2, i; //take input of the ranges printf("Enter the lower limit = "); scanf("%d", & n1); printf("Enter the upper limit = "); scanf("%d", & n2); // iterate from n1 to n2 and print the numbers printf("Numbers in the range: "); for (i = n1; i <= n2; i++) printf("%d ", i); return 0; }
Output
Case 1:
Enter the lower limit = -5
Enter the upper limit = 5
Numbers in the range: -5 -4 -3 -2 -1 0 1 2 3 4 5
Case 2:
Enter the lower limit = 13
Enter the upper limit = 30
Numbers in the range: 13 14 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 <stdio.h> // recursive function void printNumbers(int n1, int n2) { //condition for calling if (n2 > n1) printNumbers(n1, n2 - 1); // print current numbers printf("%d ", n2); } int main() { // declare variables int n1, n2, i; //take input of the ranges printf("Enter the lower limit = "); scanf("%d", & n1); printf("Enter the upper limit = "); scanf("%d", & n2); printf("Numbers in the range: "); // call the recursive function to print // all the integer numbers in a range printNumbers(n1, n2); return 0; }
Output
Case 1:
Enter the lower limit = 0
Enter the upper limit = 5
Numbers in the range: 0 1 2 3 4 5
Case 2:
Enter the lower limit = 7
Enter the upper limit = 15
Numbers in the range: 7 8 9 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 <stdio.h> int main() { // declare variables double n1, n2, in, i; //take input of the ranges printf("Enter the lower limit = "); scanf("%lf", & n1); printf("Enter the upper limit = "); scanf("%lf", & n2); // take input of step printf("Enter the value of interval = "); scanf("%lf", & in); printf("Numbers in the range: "); // dislay all numbers for (i = n1; i <= n2; i+= in) printf("%lf ", i); return 0; }
Output
Case 1:
Enter the lower limit = 10
Enter the upper limit = 15
Enter the value of interval = 0.25
Numbers in the range: 10.000000 10.250000 10.500000 10.750000 11.000000 11.250000 11.500000 11.750000 12.000000 12.250000 12.500000 12.750000 13.000000 13.250000 13.500000 13.750000 14.000000 14.250000 14.500000 14.750000 15.000000
Case 2:
Enter the lower limit = 1
Enter the upper limit = 3
Enter the value of interval = 0.33
Numbers in the range: 1.000000 1.330000 1.660000 1.990000 2.320000 2.650000 2.980000
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 <stdio.h> // recursive function void printNumbers(double n1, double n2, double in) { //condition for calling if (n2 > n1) printNumbers(n1, n2 - in, in); // print current number printf("%lf ", n2); } int main() { // declare variables double n1, n2, in; //take input of the ranges printf("Enter the lower limit = "); scanf("%lf", & n1); printf("Enter the upper limit = "); scanf("%lf", & n2); // take input of step printf("Enter the value of interval = "); scanf("%lf", & 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
Case 1:
Enter the lower limit = 10
Enter the upper limit = 15
Enter the value of interval = 0.25
Numbers in the range: 10.000000 10.250000 10.500000 10.750000 11.000000 11.250000 11.500000 11.750000 12.000000 12.250000 12.500000 12.750000 13.000000 13.250000 13.500000 13.750000 14.000000 14.250000 14.500000 14.750000 15.000000
Case 2:
Enter the lower limit = 1
Enter the upper limit = 2
Enter the value of interval = 0.1
Numbers in the range: 1.000000 1.100000 1.200000 1.300000 1.400000 1.500000 1.600000 1.700000 1.800000 1.900000 2.000000