C++ Program to Convert Number Decimal to Binary

Question : Write a C++ C++ Program to Convert Number Decimal to Binary?

Explain: C++ Language Question about conversion Number Decimal to Binary with input from user.

Formula:
A decimal number will be continually divide by (two) and a remainder will 1 or 0  until the final result equals 0 or 1 , then all result converted binary number.

for example
13
13/2=reminder 1
6/2=reminder 0
3/2=reminder 1
1

so binary number from bottom to top reminder 1101

Solution:

 /************************************************************ 
     C++ Program for Convert Number Decimal to Binary
 *************************************************************
***************** By www.cpplanguage.com *********************/

   #include <iostream>  
    using namespace std;  
    int main()  
    {  
    int array_a[10], number, c ;    
    cout<<"Enter Decimal Number: ";   
//getting input 
    cin>>number;    
    for(c=0; number>0; c++)    
    {    
// applying formula divide and divide and get reminder store in array 
    array_a[c]=number%2;    
    number= number/2;  
    }    
    cout<<"Binary Form = ";    
    for(c=c-1 ;c>=0 ;c--)    
    {    
    cout<<array_a[c];
    }    
    cout<<endl;
    } 

Output:

No comments:

Post a Comment