Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

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

상단=40+10=50

범위 - 하단 - 상단 - 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