GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — C++ Program to Convert Decimal to Binary
C++ Program to Convert Decimal to BinaryIn this article, you will learn about and get C++ code for decimal to binary conversion. The program is created using the following approaches:
Some extra programs are also available. However, before going through all of the programs available here, if you are unfamiliar with the steps used for the conversion, you can refer to Decimal to Binary to get everything you need. Decimal to Binary using an Array in C++To convert a decimal number to a binary number in C++ programming, you have to ask the user to enter the decimal number first. and then convert it into its equivalent binary value. Print its equivalent binary value on the output as shown in the program given below. The question is: write a program in C++ to receive a decimal number and print its equivalent value in binary. The answer to this question is given below: #include<iostream> using namespace std; int main() { int decimalNum, binaryNum[20], i=0; cout<<"Enter the Decimal Number: "; cin>>decimalNum; while(decimalNum!=0) { binaryNum[i] = decimalNum%2; i++; decimalNum = decimalNum/2; } cout<<"\nEquivalent Binary Value: "; for(i=(i-1); i>=0; i--) cout<<binaryNum[i]; cout<<endl; return 0; } This program was built and runs under the Code::Blocks IDE. Below is a sample run: Now supply any decimal number input, say 105, and press the ENTER key to print its equivalent value in the binary number system as shown in the output given below: The dry run of the above program with user input 105 (a decimal number) goes like this:
From 1 to 20, convert decimal to binaryHere is another program that converts all the decimal numbers from 1 to 20 to their equivalent binary values. #include<iostream> using namespace std; int main() { int decimalNum, binaryNum[10], i, k; cout<<" Decimal Number\t\tBinary Number\n"; for(k=1; k<=20; k++) { i=0; decimalNum = k; while(decimalNum!=0) { binaryNum[i] = decimalNum%2; i++; decimalNum = decimalNum/2; } cout<<" "<<k<<"\t\t\t"; for(i=(i-1); i>=0; i--) cout<<binaryNum[i]; cout<<endl; } cout<<endl; return 0; } Here is the output produced by the above program: Decimal to Binary Conversion without ArrayThe question is: write a program in C++ that converts a decimal number to a binary number without using an array. Here is its answer. This program doesn't use an array to convert any given number in decimal to its equivalent binary value. #include<iostream> using namespace std; int main() { int decimalNum, binaryNum=0, mul=1, rem; cout<<"Enter the Decimal Number: "; cin>>decimalNum; while(decimalNum>0) { rem = decimalNum%2; binaryNum = binaryNum+(rem*mul); mul = mul*10; decimalNum = decimalNum/2; } cout<<"\nEquivalent Binary Value: "<<binaryNum; cout<<endl; return 0; } Here is a sample run with 18 as a decimal number as user input: Decimal to binary conversion in C++, using a user-defined functionThis program also does the same job as the previous program. The only difference is that this program was created using a user-defined function, DecimalToBinary(). That is, this function was created to convert decimal to binary. This function takes a decimal number as its argument and returns its equivalent binary value. The return value is initialized to binaryNum (in the main() function). Print the value of "binaryNum" as output. #include<iostream> using namespace std; int DecimalToBinary(int); int main() { int decimalNum, binaryNum; cout<<"Enter the Decimal Number: "; cin>>decimalNum; binaryNum = DecimalToBinary(decimalNum); cout<<"\nEquivalent Binary Value: "<<binaryNum; cout<<endl; return 0; } int DecimalToBinary(int decimalNum) { int binaryNum=0, mul=1, rem; while(decimalNum>0) { rem = decimalNum%2; binaryNum = binaryNum+(rem*mul); mul = mul*10; decimalNum = decimalNum/2; } return binaryNum; } It will produce the same output as the previous program. You can also use user-defined functions to create the same purpose program: #include<iostream> using namespace std; int DecimalToBinary(int, int); int binaryNum[20]; int main() { int decimalNum, i; cout<<"Enter the Decimal Number: "; cin>>decimalNum; i = DecimalToBinary(decimalNum, 0); cout<<"\nEquivalent Binary Value: "; for(i=(i-1); i>=0; i--) cout<<binaryNum[i]; cout<<endl; return 0; } int DecimalToBinary(int decimalNum, int i) { while(decimalNum!=0) { binaryNum[i] = decimalNum%2; i++; decimalNum = decimalNum/2; } return i; } The same program in different languages« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |