Studytonight is now part of the GUVI universe. Explore GUVI →
🤩 New Cool Developer Tools for you. Explore →
FREE JavaScript Video Series Start Learning →
Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index
PUBLISHED ON: APRIL 18, 2022

C++ Program Convert Decimal Number To Binary Number Using Loop

We can convert any decimal number (base-10 (0 to 9)) into binary number (base-2 (0 or 1)) by C++ program.

Decimal Number

Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 223, 585, 192, 0, 7 etc.

Binary Number

Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is binary number such as 1001, 101, 11111, 101010 etc.

Decimal to Binary Conversion Algorithm In C++ Language

Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in array

Step 2: Divide the number by 2 through / (division operator)

Step 3: Repeat the step 2 until the number is greater than zero

Let's see the C++ example to convert decimal to binary.

#include <iostream> 
using namespace std; 
int main() 
{ 
int a[10], n, i; 
cout<<"Enter the number to convert: "; 
cin>>n; 
for(i=0; n>0; i++) 
{ 
a[i]=n%2; 
n= n/2; 
} 
cout<<"Binary of the given number= "; 
for(i=i-1 ;i>=0 ;i--) 
{ 
cout<<a[i]; 
} 
} 


Enter the number to convert: 9
Binary of the given number= 1001

Conclusion

Here, in this tutorial we have learned how to write and implement a C++ Program Convert Decimal Number To Binary Number Using Loop.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.

Learn to Code
Learn and practice coding side-by-side.
NEW
C language Course
115+ coding exercises
Javascript Course
85+ coding exercises

AltStyle によって変換されたページ (->オリジナル) /