logo

C의 이진수를 십진수로

이 섹션에서는 2진수를 10진수로 변환하는 방법에 대해 설명합니다. 개념에 들어가기 전에 이진수와 십진수에 대한 이해가 필요합니다. 우리가 알고 있듯이 컴퓨터는 인간이 쓰거나 행하는 단어나 숫자를 이해하지 못합니다. 대신 0과 1만 이해합니다. 예를 들어, 컴퓨터에 단어나 숫자를 입력하면 다양한 소프트웨어나 컴파일러가 이러한 숫자나 단어를 이진 형식(0과 1비트)으로 변환하는 데 도움을 줍니다. 컴퓨터 기계가 이를 쉽게 이해할 수 있도록 말이죠.

지도 머신러닝
C의 이진수를 십진수로

이진수

이진수(Binary Number)는 컴퓨터에 저장되어 있는 정보나 데이터를 0과 1의 비트로 조합하여 나타내는 숫자이다. 0과 1의 두 비트를 갖기 때문에 2진수 시스템이라고도 합니다. 이는 이진수(0과 1) 1001, 1010, 1101, 1111, 1010101 등입니다.

십진수

십진수는 0부터 9까지 10개의 숫자로 구성된 숫자입니다. 10개의 숫자(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)를 모아서 다음을 나타내거나 만들기 때문에 밑이 10입니다. 이 10자리 숫자를 사용하여 정수를 만듭니다.

2진수를 10진수로 변환하는 알고리즘

  1. 입력으로 이진수를 사용합니다.
  2. 숫자를 10으로 나누고 나머지를 변수 rem에 저장합니다.
  3. 십진수_숫자 = 십진수_숫자 + rem * 기본;
    처음에 십진수는 0이고 밑은 1입니다. 여기서 rem 변수는 숫자의 나머지 부분을 저장합니다.
  4. 원래 숫자의 몫을 10으로 나눕니다.
  5. 밑수에 2를 곱합니다.
  6. 이진수의 십진수를 인쇄합니다.

while 루프를 사용하여 이진수를 십진수로 변환

while 루프를 사용하여 이진수(0과 1)의 조합을 십진수로 변환하는 C 프로그램을 고려해 보겠습니다.

프로그램.c

 #include #include void main() { // declaration of variables int num, binary_num, decimal_num = 0, base = 1, rem; printf (' Enter a binary number with the combination of 0s and 1s 
'); scanf (' %d', &num); // accept the binary number (0s and 1s) binary_num = num; // assign the binary number to the binary_num variable while ( num > 0) { rem = num % 10; /* divide the binary number by 10 and store the remainder in rem variable. */ decimal_num = decimal_num + rem * base; num = num / 10; // divide the number with quotient base = base * 2; } printf ( ' The binary number is %d 	', binary_num); // print the binary number printf (' 
 The decimal number is %d 	', decimal_num); // print the decimal getch(); } 

산출

 Enter a binary number with the combination of 0s and 1s 1101 The binary number is 1101 The decimal number is 13 

코드 설명: 위 프로그램에서 볼 수 있듯이 사용자에게 이진수(0과 1)를 요청하여 해당 숫자를 변수 num에 저장합니다. 각 반복에서 while 루프는 이진수 조건을 확인하고 주어진 숫자가 0보다 작지 않아야 하는지 확인합니다. 그렇지 않으면 루프에서 종료됩니다.

다음은 while 루프의 반복입니다.

첫 번째 반복:

렘 = 1101 % 10 => 1

십진수 = 0 + 1 * 1 => 1 (십진수 값 = 0, rem = 1, 밑수 = 1)

정렬된 배열 목록

숫자 = 1101 / 10 => 110

기본 = 1 * 2 => 2

두 번째 반복:

렘 = 110% 10 => 0

십진수 = 1 + 0 * 2 => 1 (십진수 값 = 1, rem = 0, 밑수 = 2)

모두 다 바꿔

숫자 = 110 / 10 => 11

기본 = 2 * 2 => 4

세 번째 반복:

렘 = 11% 10 => 1

십진수 = 1 + 1 * 4 => 5 (십진수 값 = 1, rem = 1, 밑수 = 4)

숫자 = 11 / 10 => 1

기본 = 4 * 2 => 8

4번째 반복:

렘 = 1% 10 => 1

십진수 = 5 + 1 * 8 => 1 (십진수 값 = 5, rem = 1, 밑수 = 8)

숫자 = 1 / 10 => 0

기본 = 8 * 2 => 16

C의 행렬 곱셈

for 루프를 사용하여 이진수를 십진수로 변환

for 루프를 사용하여 이진수(0과 1)의 조합을 십진수로 변환하는 C 언어 프로그램을 고려해 보겠습니다.

십진수.c

 #include #include #include // use math.h header file void main() { // declaration of local variables i, bin_num, decimal_num = 0, rem; int i, bin_num, decimal_num = 0, rem; printf (' Enter the binary number with the combination of 0s and 1s 
'); scanf ('%d', &bin_num); // takes the binary number as the input printf( ' 
 The binary number is %d', bin_num); // print the binary number for (i = 0; bin_num != 0; ++i) { rem = bin_num % 10; bin_num = bin_num / 10; decimal_num = decimal_num + (rem) * ( pow (2, i)); } // print the decimal number printf ('
 Conversion from binary to decimal number is %d', decimal_num); getch(); } 

산출

 Enter the binary number with the combination of 0s and 1s 10010 The binary number is 10010 Conversion from binary to decimal number is 18 

함수를 사용하여 이진수를 십진수로 변환

사용자 정의 함수를 사용하여 이진수(0과 1)의 조합을 십진수로 변환하는 C 언어 프로그램을 생각해 보겠습니다.

그래서.c

자바로 파일을 여는 방법
 #include #include int binaryTodecimal(int bin_num); int main() { // declare the local variable int bin_num, dec_num; printf (' Enter the binary number (0s and 1s) 
'); scanf ('%d', &bin_num); dec_num = binaryTodecimal (bin_num); // call the binaryTodecimal() function printf (' Conversion of the binary number to decimal number is %d', dec_num); } // use user defined function --- binaryTo decimal function int binaryTodecimal( int bin_num) { // declaration of variables int decimal_num = 0, temp = 0, rem; while (bin_num != 0) { rem = bin_num % 10; bin_num = bin_num / 10; decimal_num = decimal_num + rem * pow( 2, temp); temp++; } return decimal_num; } 

산출

 Enter the binary number (0s and 1s) 11001 Conversion of the binary number to decimal number is 25 

배열과 함수를 사용하여 이진수를 십진수로 변환

함수와 배열을 사용하여 이진수(0과 1)의 조합을 십진수로 변환하는 C 언어 프로그램을 생각해 보겠습니다.

Decimal2.c

 #include #include int binaryTodecimal (char num[]) { int i, deci_num, mul = 0; for ( deci_num = 0, i = str_length(num) - 1; i >= 0; --i, ++mul) { deci_num = deci_num + (num[i] - 48) * (1 << mul); } return deci_num; } int str_length( char str[]) { int i = 0; while (str[i] != '') i++; return i; } int main() { char num[] = '1101'; int deci_num; printf ('
 The binary number is %s', num); printf ('
 The decimal number of %s is %d', num, binaryTodecimal(num)); return 0; } 

산출

 The binary number is 1101 The decimal number of 1101 is 13