Decimal To Binary in C
Q. Write a C program decimal to binary ?
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i;
int b[1000];
printf("Enter an integer in decimal number system:");
scanf("%d", &n);
for (i=0;n>0;i++)
{
b[i]=n%2;
n=n/2;
}
printf("%d in binary number system is:" ,n);
for(int j=i-1;j>=0;j--)
{
printf("%d",b[j]);
}
}
Q. Write an algorithm to decimal to binary ?
1. Input:
- Read an integer `n` from the user.
2. Initialize:
- Create an array `b` to store binary digits.
- Initialize a counter `i` to 0.
3. Convert Decimal to Binary:
- Repeat while `n` is greater than 0:
- Calculate the remainder of `n` divided by 2 (`n % 2`) and store it in `b[i]`.
- Update `n` by dividing it by 2 (`n = n / 2`).
- Increment `i`.
4. Output:
- Print the binary digits stored in the array `b` in reverse order (from `b[i-1]` down to `b[0]`).
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com