Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to print the range of numbers using C language?
In C programming, finding the range of a given number means determining the interval of 10 consecutive numbers within which that number falls. For example, if the number is 45, it falls in the range 40-50.
Syntax
lower = (n/10) * 10; upper = lower + 10;
How It Works
The logic to find the range uses integer division to extract the tens digit −
-
Lower bound:
(n/10) * 10removes the units digit and gives the nearest lower multiple of 10 -
Upper bound:
lower + 10gives the next multiple of 10
For example, if n = 45 −
- Lower = (45/10) * 10 = 4 * 10 = 40
- Upper = 40 + 10 = 50
- Range = 40-50
Example 1: Basic Range Finder
Following is the C program for printing the range of numbers −
#include <stdio.h>
int main() {
int n, lower, upper;
printf("Enter a number: ");
scanf("%d", &n);
lower = (n/10) * 10; /* Integer division removes units digit */
upper = lower + 10;
printf("Range is %d - %d<br>", lower, upper);
return 0;
}
Enter a number: 25 Range is 20 - 30
Example 2: Range for Larger Numbers
This example demonstrates range calculation for three-digit numbers −
#include <stdio.h>
int main() {
int number, start, end;
printf("Enter a number: ");
scanf("%d", &number);
start = (number/10) * 10; /* Calculate lower bound */
end = start + 10; /* Calculate upper bound */
printf("Number %d falls in range %d - %d<br>", number, start, end);
return 0;
}
Enter a number: 457 Number 457 falls in range 450 - 460
Key Points
- Integer division
(n/10)automatically truncates the decimal part - This method works for positive numbers of any size
- The range always spans exactly 10 consecutive numbers
Conclusion
Finding the range of a number in C uses simple arithmetic operations with integer division. The formula (n/10) * 10 efficiently calculates the lower bound of any number's decade range.
Advertisements
