Our Blog Contain Detail about Some Technical Aspect like Programming, Blogger, Tools and Tip, Suggestion, Motivational, Health, Program in C and Java, Html

Write a program to convert decimal number in to binary number.

 Write a program to convert decimal number in to binary number

The logic to convert a decimal number to binary involves dividing the decimal number by 2 and storing the remainder in a separate container. The division is performed repeatedly until the quotient becomes zero. The binary equivalent of the decimal number is obtained by arranging the remainders from bottom to top.



Here is an example of how to convert the decimal number 13 to binary:

Divide 13 by 2 to get the quotient 6 and remainder 1. Store the remainder 1 in a container.

Divide 6 by 2 to get the quotient 3 and remainder 0. Store the remainder 0 in the container.

Divide 3 by 2 to get the quotient 1 and remainder 1. Store the remainder 1 in the container.

Divide 1 by 2 to get the quotient 0 and remainder 1. Store the remainder 1 in the container.

The binary equivalent of 13 is obtained by arranging the remainders from bottom to top: 1101.

So, the decimal number 13 in binary is 1101.

This logic can be implemented in programming languages like Python, C, Java, and others using loops, if statements, and operators. The process of converting a decimal number to binary is a common task in computer programming and is used in many applications, including digital electronics and networking.

Algorithm for Convert Decimal Number in to Binary Number

Here's an algorithm to convert a decimal number to its binary equivalent:

Step1: Read the decimal number.

Step3: Initialize a binary_number variable to an empty string.

Step4: If the decimal number is 0, return 0 as the binary equivalent.

Step5: While the decimal number is greater than 0, do the following:

a. Calculate the remainder when the decimal number is divided by 2 using the modulus operator.

b. Convert the remainder to a string and append it to the beginning of the binary_number string variable.

c. Update the decimal number by dividing it by 2 and discarding any remainder using integer division.

Step6: Return the binary_number as the binary equivalent of the decimal number.

This algorithm can be implemented in any programming language of your choice.

Here's a C program to convert a decimal number to its binary equivalent:

#include <stdio.h>

int main() {

    int decimal_number, quotient, i = 1, j;

    int binary_number[100];

 

    printf("Enter a decimal number: ");

    scanf("%d", &decimal_number);

 

    quotient = decimal_number;

 

    while (quotient != 0) {

        binary_number[i++] = quotient % 2;

        quotient = quotient / 2;

    }

    printf("The binary equivalent is: ");

    for (j = i - 1; j > 0; j--) {

        printf("%d", binary_number[j]);

    }

    printf("\n");

    return 0;

}

Explanation:

·       The program declares variables for the decimal number, the quotient, and two counter variables, i and j.

·       The program declares an array called binary_number that will hold the binary equivalent of the decimal number.

·       The program prompts the user to enter a decimal number using the printf() and scanf() functions.

·       The program sets the quotient variable to the decimal number.

·       The program enters a while loop that runs as long as the quotient is not 0.

·       Inside the loop, the program calculates the remainder when the quotient is divided by 2 using the modulus operator %.

·       The program stores the remainder in the binary_number array starting from the least significant bit and moving towards the most significant bit.

·       The program updates the quotient by dividing it by 2 and discarding any remainder using integer division /.

·       When the while loop terminates, the program prints the binary equivalent of the decimal number by iterating through the binary_number array from the most significant bit to the least significant bit and printing each bit using the printf() function.

Example output:

Enter a decimal number: 13

The binary equivalent is: 1101


Share:

No comments:

Post a Comment

Translate

Followers

Email Subscription

Enter your email address:

Delivered by FeedBurner

Recent Posts

Theme Support

Need our help to upload or customize this blogger template? Contact me with details about the theme customization you need.