Home  >  Article  >  Backend Development  >  How to print a range of numbers using C language?

How to print a range of numbers using C language?

WBOY
WBOYforward
2023-08-29 08:05:05800browse

How to print a range of numbers using C language?

Question

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

Solution

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

The logic we apply to find the range is −

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

Explanation

Let the number n=45

Lower limit=(42/10)*10 / / Division returns quotient

=4*10 =40

upper limit=40 10=50

range−lower limit-upper limit−40-50

Example

The following is a C program for printing a 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 results −

Enter a number:25
Range is 20 &ndash; 30

This is another C program for printing a 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 results −

Enter a number:457
Range is 450 &ndash; 460

The above is the detailed content of How to print a range of numbers using C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete