Home > Backend Development > C++ > body text

How to print a range of numbers using C language?

WBOY
Release: 2023-08-29 08:05:05
forward
810 people have browsed it

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;
Copy after login

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();
}
Copy after login

Output

When the above program is executed, it produces The following results −

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

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();
}
Copy after login

Output

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

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template