C Online Compiler
Example: C program to convert decimal to binary using a function
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C program to convert decimal to binary using a function #include <stdio.h> // Step-1 Function to convert decimal to binary long long convertToBinary(int num) { long long binary = 0; int place = 1; // Convert decimal to binary using loop while(num != 0) { binary += (num % 2) * place; num /= 2; place *= 10; } return binary; } // The main function of the program int main() { // Step-2 Declare variables int num; long long binary; // Step-3 Input a decimal number from the user printf("Enter a decimal number: "); scanf("%d", &num); // Step-4 Call the function and store the result binary = convertToBinary(num); // Step-5 Output the result printf("Binary representation: %lld\n", binary); return 0; }
13
Output
Clear
ADVERTISEMENTS