C Program : Learn to find bunary number for any decimal number

in hive-175254 •  2 years ago  (edited)

Welcome back,
In today's blog we will learn to find out the equivalent binary number for the decimal input by the user.

If you have studied computer science in your school or college then this is the topic you must have learnt in your initial years.

So here is the program for find the equivalent binary number for a decimal number input by the user.

Screenshot 2023-02-10 214156.png

You can copy the code from below :

#include <stdio.h>
#include <math.h>

int main() {
int decimal, binary = 0, i = 0;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
while (decimal != 0) {
binary = binary + (decimal % 2) * pow(10, i);
decimal = decimal / 2;
i++;
}
printf("The binary equivalent of the decimal number is: %d", binary);
return 0;
}

When you run this program, it will prompt you to enter a decimal number. After you enter the decimal number, the program will calculate its binary equivalent and print the result.

Let us look at the output for the program we just wrote above.

Screenshot 2023-02-10 214205.png

In this program, the while loop performs the conversion by repeatedly dividing the decimal number by 2 and storing the remainder in the binary equivalent. The pow function from the math.h library is used to calculate the correct place value for each binary digit.

Screenshot 2023-02-10 214235.png

When 35 is entered as the decimal number, the program calculates its binary equivalent by dividing it by 2 repeatedly and adding the remainders to the binary equivalent using the pow function. Finally, the binary equivalent is printed as 100011.

So we learnt to find binary number for a decimal number input by the user in this blog today.

If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.

I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.

Source

I am happy to call myself a trader and small programmer at the same time now.

Happy trading and keep learning what you love.

image.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

@tipu curate