How to print the range of numbers using C language?


Problem

For given number try to find the range in which that number exists.

Solution

Here, we are learning how to find the ranges of a number.

The logic that we apply to find range is −

lower= (n/10) * 10; /*the arithmetic operators work from left to right*/
upper = lower+10;

Explanation

Let number n=45

Lower=(42/10)*10 // division returns quotient

       =4*10 =40

Upper=40+10=50

Range − lower-upper − 40-50

Example

Following is the C program for printing the range of numbers

#include<stdio.h>
main(){
   int n,lower,upper;
   printf("Enter a number:");
   scanf("%d",&n);
   lower= (n/10) * 10; /*the arithmetic operators work from left to right*/
   upper = lower+10;
   printf("Range is %d - %d",lower,upper);
   getch();
}

Output

When the above program is executed, it produces the following result −

Enter a number:25
Range is 20 – 30

Here is another C program for printing the range of numbers.

#include<stdio.h>
main(){
   int number,start,end;
   printf("Enter a number:");
   scanf("%d",&number);
   start= (number/10) * 10; /*the arithmetic operators work from left to right*/
   end = start+10;
   printf("Range is %d - %d",start,end);
   getch();
}

Output

When the above program is executed, it produces the following result −

Enter a number:457
Range is 450 – 460

Updated on: 13-Mar-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements