C Program to Convert Decimal Number to Binary Number

preview_player
Показать описание
This program is to convert decimal to binary in C. After watching this video, you will be able to convert any decimal number to binary in C

Best Hindi Videos For Learning Programming:

►C Language Complete Course In Hindi -

►JavaScript Complete Course In Hindi -

►Django Complete Course In Hindi -

Follow Me On Social Media
Рекомендации по теме
Комментарии
Автор

Last semester, I made a program to convert from base 10 to base n, but I didn't know how to convert fractional numbers. I was inspired by your video title to create a program that can accept rational inputs like 32.47. I first sat down and figured out the algorithm to convert the given number's integer and fractional parts to binary on paper (using reminders and division for the integer part, and multiplication and subtraction for the fractional part). Then, I generalized it to base n and wrote the program in C.Since higher bases need more digits than 0-9, I used letters A-Z and a-z to represent higher bases.
To handle negative values, the program will print "-" and then take the absolute value and proceed.

Here is the program. Please feel free to read it and suggest improvements. I have tried to include extensive comments for the reader's convenience.



#include<stdio.h>
#include<stdlib.h>
char digit(int d1){//convert given number to it's corresponding digit
if (d1 < 10)
return(d1 + 48); //use digits [0 to 9]
else if (d1 <= 35 && 10 <= d1)
return(d1 + 55); //use Capital case latin alphabets [A to Z] for digits [10 to 36]
else if (d1 <= 62 && 35 < d1)
return(d1 + 61); //use lower case latin alphabets [a to z] for digits [37 to 62]
else
exit(1);
}

void convert(long double num, int base, int acc){//converts base 10 to base n
//num is the number to be converted
//base is the base to be converted to
//acc is the number of fractional digits
if(base<2){//bases 0 and 1 are non-sensical
printf("can't convert");//I hope to one day handle negitive bases
return;
}
if(num<0){//if given number is negitive,
printf("-");//keep a "-" sign in front of the string
num=-num;//take the absolute value and then procced
}
int intpart=(int)num;//is the integer part of given number
long double fracpart=num-(long double)intpart;//is the fractional part
//begin converting integer part
int i=0;
if(base<62){//if bases less than 62
char* array to store the digits
while(intpart!=0){
gives the ith digit(from ones place)
intpart=intpart/base;//divide number by base and repeat
i++;
}
for(i=i-1;i>=0;i--){//print array in reverse order
printf("%c", D[i]);//this is because ones place is the first entry of array
}

//now do frac part
for(i=0;i<acc;i++){
number by the base
int n=(int)fracpart;//the digit on the ones place will be the digit
D[i]=digit(n);//re-use the D array used previously for integer part
fracpart=fracpart-(long double)n;//subtract by that digit, make ones place zero
}
printf(".");//print the point
for(i=0;i<acc;i++){
printf("%c", D[i]);//print the array in
}

free(D);
}
else if(base>=62){//use mixed ratix
int*
while(intpart!=0){//same method as before except the digits will be represented in base 10
Dint[i]=intpart%base;//and then seperated by ", "
intpart=intpart/base;
i++;
}
for(i=i-1;i>=0;i--){
printf("%d, ", Dint[i]);
}

//now do frac part
for(i=0;i<acc;i++){
fracpart=base*fracpart;
int n=(int)fracpart;
Dint[i]=n;
fracpart=fracpart-(long double)n;
}
printf("\b;");//";" instead of "."
for(i=0;i<acc;i++){
printf("%d, ", Dint[i]);
}
printf("\b ");

free(Dint);
}
}


(i did not include the main function as that is not relevent)

Thank you bhayia for the motivation. I had no idea what to do in my free time as I am now in vacation and was bored. Seeing this video reminded me that I have'nt yet made an updated base converter program

munol
Автор

The binary form of 12 will be 1100. Is it correct?

eldrago
Автор

9:45 --> here ✓ is used for division.
2✓12 = 0, 2✓6 = 0, 2✓3 = 1, 2✓1 = 1
Finally 12 ----> 1100.

Aryavart..
Автор

Thankyou sir you teaches very well..now no confusion about this programme 😊

kajalgoswami
Автор

Thank you Aapke har eak videos Jo Maine dekhe hai, sabse kuch sikhaa huu, aur aage bhi sikungaa

darshan_the_admin
Автор

Please make a playlist on Java
Because in my book java is there but I can't understand
Please make a playlist on java
Please make a playlist on java

Thanking you you and your work

hdgamer
Автор

You are my best and favourite teacher in the world

StatusWorld-bppq
Автор

9.34
For decimal to binary
12 -> 1100

ronakmaniya
Автор

Sir i have almost complited the web development playlist.... But i have basic knolwde of php so can you please make a video on how to use php as a I felt php is easy for a beginner rather than node So sirplease make a video on " Using PHP In backend....

krishnachaudhary
Автор

Thanks for these amazing videos... Can you please make videos covering other conversions and other questions which are common in online tests for internships and

Waiting!!!

cristinarosales
Автор

Aaj hi ye digital electronics me pdhaya gya tha😂
Everytime u match 😁👍

harshitachaurasia
Автор

16:45
int i=4;
while(n) {
rem = n%2;
n=n/2
a[i]= rem
i - - ;
}
Can we use this instead?
Reply if u read this comment.

yashwanth
Автор

harry bhai thank you for this special trick.
mene 12 ka binary sy decimal ki h.
the answer is=(1100)

t-w-s
Автор

I am very beginner, everything I have leaned from u. I have written the java code for this question.
package project1;

import java.util.Scanner;
public class DecToBin {
public static void main(String[] args){

int[] arr = new int [10];
Scanner input = new Scanner(System.in);
System.out.println("Enter your desire Number");
int n = input.nextInt();
int reminder;
int i=0;
while(n!=0){
reminder = n % 2;
n = n/2;
arr[i] = reminder;
i++;
}
System.out.println("The Binary is");
for(int j=i-1;j>=0;j--)
{
System.out.print(arr[j]);
}
System.out.println();
}
}

subhajitghosh
Автор

hii Harry, please make a video on number system including hexadecimal, octal number system. As soon as possible .

everydayamazingofficial
Автор

Bhaiya you are great and you upload informative videos...

asparshraj
Автор

Can you please do a live/recorded freelancer project for a software developer, like using python and django or according to the project requirements ?

pythonenthusiast
Автор

I am bangladeshi.i think your youtube channel is the best for coding

darkangle
Автор

9:43 1100 in binary sir you are the best if you see this comment please reply and heart

snorlaxcodes
Автор

Thanks a lot Harry sir iss code ko samjhane ke liye..

rs__