首页  >  文章  >  后端开发  >  如何使用C语言打印数字范围?

如何使用C语言打印数字范围?

WBOY
WBOY转载
2023-08-29 08:05:05748浏览

如何使用C语言打印数字范围?

问题

对于给定的数字,尝试找到该数字存在的范围。

解决方案

这里,我们正在学习如何找到一个数字的范围。

我们应用于找到范围的逻辑是 −

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

Explanation

让数字n=45

下限=(42/10)*10 // 除法返回商

       =4*10 =40

上限=40+10=50

范围 − 下限-上限 − 40-50

Example

以下是用于打印数字范围的C程序 −

#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();
}

输出

当上述程序被执行时,它会产生以下结果 −

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

这是另一个用于打印数字范围的C程序。

#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();
}

输出

当上述程序被执行时,它会产生以下结果 −

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

以上是如何使用C语言打印数字范围?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除