如何用 C 語言列印數字範圍?
問題
對於給定的數字,請嘗試找出該數字存在的範圍。
解決方案
在這裡,我們將學習如何查詢數字的範圍。
我們用來查詢範圍的邏輯是 -
lower= (n/10) * 10; /*the arithmetic operators work from left to right*/ upper = lower+10;
說明
假設數字 n=45
Lower=(42/10)*10 // 除法會返回商
=4*10 =40
Upper=40+10=50
Range − lower-upper − 40-50
示例
以下是用於列印數字範圍的 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 – 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 – 460
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP