C 프로그래밍 언어에서 소수를 이진 분수로 변환하는 방법을 이해하려면 아래의 예를 고려하십시오.
예시 1 − 25를 이진수로 변환합니다.
Step 1 − 25 / 2 Rem :1 , Quo :12
Step 2 − 12 / 2 Rem :0 , Quo :6
Step 3 − 6 / 2 Rem :0 , Quo :3
Step 4 − 3 / 2 Rem :1 , Quo :1
5단계 − 1 / 2 Rem:1 , Quo:0
따라서 동등한 이진수는 11001
입니다.예시 2 − 0.7에서 바이너리로 변환
1단계 − 0.7 * 2 =1.4, Int 부분 =1
2단계 − 0.4 * 2 =0.8, Int 부분 =0
3단계 − 0.8 * 2 =1.6, Int 부분 =1
4단계 − 0.6 * 2 =1.2, Int 부분 =1
5단계 − 0.2 * 2 =0.4, Int 부분 =0
6단계 − 0.4 * 2 =0.8, Int 부분 =0
따라서 동등한 이진수는 0.101100
입니다.3단계 - 마지막으로 십진수 25.7의 이진수 값은 다음과 같습니다. -
11001 + 0.101100 = 1101.101100
예시
다음은 소수 분수를 이진 분수로 변환하는 C 프로그램입니다. -
#include<stdio.h>
int main(){
long double fraDecimal,fraBinary,bFractional = 0.0,dFractional,fraFactor=0.1;
long int dIntegral,bIntegral=0;
long int intFactor=1,remainder,temp,i;
printf("Enter any fractional decimal number: ");
scanf("%Lf",&fraDecimal);
dIntegral = fraDecimal;
dFractional = fraDecimal - dIntegral;
while(dIntegral!=0){
remainder=dIntegral%2;
bIntegral=bIntegral+remainder*intFactor;
dIntegral=dIntegral/2;
intFactor=intFactor*10;
}
for(i=1;i<=6;i++){
dFractional = dFractional * 2;
temp = dFractional;
bFractional = bFractional + fraFactor* temp;
if(temp ==1)
dFractional = dFractional - temp;
fraFactor=fraFactor/10;
}
fraBinary = bIntegral + bFractional;
printf("Equivalent binary value: %lf",fraBinary);
return 0;
} 출력
위의 프로그램을 실행하면 다음과 같은 결과가 나온다 -
Enter any fractional decimal number: 5.7 Equivalent binary value: 101.101100