In this tutorial you will learn about the C Program to Convert Decimal to Binary and its application with practical example.
C Program to Convert Decimal to Binary
In this tutorial, we will learn to create a C program that will Convert Decimal to Binary in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C programming.
Program to Convert Decimal to Binary:-
As we all know the c is a very powerful language. With the help of c programming language, we can make many programs. We cal perform many input-output operations using c programming. In today’s tutorial, we take the input in Decimal from the user and convert it into Binary. With the help of c programming, we can perform many conversion operations.
With the help of this program, we can Convert Decimal to Binary.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Takeing the input number from the user in decimal for the program. 3. Passing that input to the string function. 4. Pass that number to a for loop for convertion. 4. Print the Result. 5. End the program. |
Program to Convert Decimal to Binary:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* C Program to Convert Decimal to Binary */ #include <stdio.h> #include <math.h> /* USer defined function for the covertion Decimal to Binary */ long long convert(int); int main() { /* Declaring the required local variable for the program */ int n, bin; /* Taking input from the User in the form or a decimal number */ printf("Enter a decimal number: "); scanf("%d", &n); /* Calling the function for the convertion of decimal number to a binary number */ bin = convert(n); /* Printing the converted output number with the help of function */ printf("%d in decimal = %lld in binary", n, bin); return 0; } /* Function body for the convertion from decimal number to a binary number */ long long convert(int n) { long long bin = 0; int rem, i = 1; while (n!=0) { rem = n % 2; n /= 2; bin += rem * i; i *= 10; } return bin; } |
Output:-
In the above program, we have first initialized the required variable.
- bin= it will hold the integer value.
- n = it will hold the integer value.
Taking the input from the message.