C++ Program to Convert Decimal to Binary
Here you will get the source code of C++ Program to Convert Decimal to Binary number by using while loop.
Example: C++ Program to Convert Decimal to Binary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> using namespace std; int main() { int n,j,rem=1,num; int binaryNum[32]; cout<<"Enter a number : "; cin>>n; int i = 0; num=n; while (n!=0) { rem = n % 2; n = n / 2; binaryNum[i]=rem; i++; } i--; cout<<"Binary number of "<<num<<" is "; while(i>=0) { cout<<binaryNum[i]; i--; } return 0; } |
Output 1
Enter a number : 6
Binary number of 6 is 110
Output 2
Enter a number : 16
Binary number of 16 is 10000
Check out our other C++ programming Examples