C++ Online Compiler
Example: C++ program to convert decimal to binary using function
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// C++ program to convert decimal to binary using function #include <iostream> using namespace std; // Function to convert decimal to binary long long convertToBinary(int num) { long long binary = 0; int place = 1; // Convert to binary using mathematical logic while (num != 0) { int remainder = num % 2; binary += remainder * place; num /= 2; place *= 10; } return binary; } // The main function of the program int main() { // Step-1 Declare variables int number; // Step-2 Input decimal number cout << "Enter a decimal number: "; cin >> number; // Step-3 Call the function to convert long long result = convertToBinary(number); // Step-4 Output the result cout << "Binary representation: " << result << endl; return 0; }
Output
Clear
ADVERTISEMENTS