How To Write a C Program To Convert Decimal Number Into Binary Number in C Programming Language ?
Solution For C Program To Convert Decimal Number Into Binary Number:
#include<stdio.h> #include<conio.h> #include<math.h> void dec_bin(long int num) // Function Definition { long int rem[50],i=0,length=0; while(num>0) { rem[i]=num%2; num=num/2; i++; length++; } printf("Binary number : "); for(i=length-1;i>=0;i--) printf("%ld",rem[i]); } //================================================ void main() { long int num; clrscr(); printf("Enter the decimal number : "); scanf("%ld",&num); dec_bin(num); // Calling function getch(); }Output :Enter the decimal number : 7 Octal number : 111
Explanation of Program :
Always Program execution starts from main function. Inside Main function we are accepting the decimal number.
printf("Enter the decimal number : "); scanf("%ld",&num);
Now after accepting the input decimal number we pass the decimal number to function dec_bin(num) function using following syntax.
dec_bin(num); // Calling function
Inside the Function Call -
We have declared some variables and the use of each variable is listed in following table -
Variable | Data Type | Initial Value | Use of Variable |
---|---|---|---|
rem[50] | long int | Garbage | Storing the Remainder |
i | long int | 0 | Subscript Variable for Running Loop |
length | long int | 0 | Storing the Length of the Array |
Step 1 : Check Whether the Number is Less than or Equal to Zero
Step 2 : Divide the number by 2 and store the remainder in the array
while(num>0) { rem[i]=num%2; num=num/2; i++; length++; }
Step 3 : Increase the length of the array by 1 and also increment the subscript variable
After the execution of while loop print the array in reverse order.Tags:C Program To Convert Decimal Number Into Binary Number, c program to convert binary to decimal using array, simple c program to convert decimal to binary, write a c program to convert decimal number to binary number, c program to convert decimal into binary octal and hexadecimal, c program to convert decimal to binary without using array, c program to convert decimal to binary using function, c program to convert decimal to binary using stack, c program to convert decimal to binary using bitwise operators.
Learn More :
C Program
- Using Bash to input stuff into c program
- Difficult C Programming Questions
- Write a c program to find largest among three numbers using binary minus operator three numbers using binary minus operator
- PRINTING ASCII VALUE USING C PROGRAM
- MULTIPLICATION OF TWO MATRICES USING C PROGRAM
- FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING
- Write A C Program To Find Out Transport Of A Matrix
- Factorial of 100 in C Program
- Multiplication of large numbers in c
- Division of Large Numbers in C Program
- BINARY SEARCH USING C PROGRAM
- BINARY SEARCH THROUGH RECURSION USING C PROGRAM
- FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
- FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
- FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
- FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
- REVERSE A NUMBER USING RECURSION IN C PROGRAM
- SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
- Write A C Program For Swapping Of Two Arrays
- SWAPPING OF STRINGS USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
- CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM
- C or C++ Program To Find Bonus Amount