C Program : Learn to find bunary number for any decimal number
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.
You can copy the code from below :
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.
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.
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.
I am happy to call myself a trader and small programmer at the same time now.
Happy trading and keep learning what you love.
@tipu curate
Upvoted 👌 (Mana: 3/5) Get profit votes with @tipU :)