Q-28: Write a C/C++ program to convert a given decimal number to its equivalent binary number.
    

Using - C


#include<stdio.h> void convertToBinary(int num); void main() { int num; printf("Enter a number: "); scanf("%d",&num); printf("DECIMAL: %d",num); convertToBinary(num); } void convertToBinary(int num) { int binaryArray[1024], remainder, i=0, j; while(num != 0) { remainder = num % 2; binaryArray[i++] = remainder; num /= 2; } printf("\nBINARY : "); for(j=i-1; j >= 0; j--) { printf("%d", binaryArray[j]); } }

OUTPUT

Enter a number:  46
DECIMAL:  46
BINARY   :  101110

        

Using - C++


#include<iostream> using namespace std; void convertToBinary(int num); int main() { int num; cout<< "Enter a number: "; cin>> num; cout<< "DECIMAL: "<< num; convertToBinary(num); return 0; } void convertToBinary(int num) { int binaryArray[1024], remainder, i=0; while(num != 0) { remainder = num % 2; binaryArray[i++] = remainder; num /= 2; } cout<< "\nBINARY : "; for(int j=i-1; j >= 0; j--) { cout<< binaryArray[j]; } }

OUTPUT

Enter a number:  8
DECIMAL:  8
BINARY   :  1000