Here you will get program to convert a number from decimal to binary in C++.
How to convert decimal number to binary?
- Divide the number by 2 and save the remainder somewhere.
- Divide the quotient with 2 again and save the remainder somewhere.
- Repeat the process until 1 is left as quotient.
- Now write the remainders in reverse order.
This program can only convert non decimal numbers.
Also Read: Convert Binary to Decimal in C++
Program for Decimal to Binary in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include<iostream> using namespace std; int main() { int d,n,i,j,a[50]; cout<<"Enter a number:"; cin>>n; cout<<"\nThe binary conversion of "<<n<<" is 1"; for(i=1;n!=1;++i) { d=n%2; a[i]=d; n=n/2; } for(j=i-1;j>0;--j) cout<<a[j]; return 0; } |
Output
Enter a number:5
The binary conversion of 5 is 101
The binary conversion of 5 is 101
this Not what iam scerching
write a program that print n decimal number with their binary code respective