Saturday, September 24, 2022

C Program To Convert Decimal Number To Binary Number without using array

Method to calculate Decimal to Binary Number

Take a decimal input from the user

for example :

if user enter a decimal 12

We keep on dividing the number 12 by 2.

 

12 / 2 = 6, reminder 0

06 / 2 = 3, reminder 0

03 / 2 = 1, reminder 1.

01/ 2 = 1, reminder 1.

Decimal to binary

So Binary equivalent of 12 is 1100.



Write all reminders bottom to top. It’s binary equivalent of decimal (12)10=(1100)02

Source Code :

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int t, n, r=0, p=1, bin=0;

printf("Enter a number\n");

scanf("%d",&n);

t=n;

while(n>0)

{

r=n%2;

n=n/2;

bin=bin+(r*p);

p=p*10;

}

printf("Binary of decimal %d = %d",t,bin);

getch();

}

YouTube Video :



 

No comments:

Post a Comment