Decimal to Binary Display

Decimal to Binary Display

Hard C Control Flow 27 views
Explanation Complexity

Problem Statement

Take a decimal number from the user (validation: positive integer only). Then convert it to binary and print it, also showing the step-by-step conversion process.

Input Format

A single positive integer entered by the user.


Output Format

The binary equivalent of the number, along with the step-by-step conversion process.

Example

13
Binary: 1101

Constraints

• Input must be a positive integer

• Decimal number ≥ 1

Concept Explanation

Decimal to binary conversion is done by repeatedly dividing the number by 2 and noting the remainders.

Step-by-Step Explanation

1.Read the number from the user.

2.Check validation:

• If the number is less than or equal to 0, stop and show invalid input.

3. Initialize an empty list (or string) to store remainders.

4. While the number is greater than 0:

• Divide the number by 2.

• Store the remainder (number % 2).

• Update the number to the quotient (number / 2).

5.The remainders collected are in reverse order.

6.Reverse the stored remainders.

7.Print the reversed sequence as the binary number.

Concept Explanation

Decimal to binary conversion is done by repeatedly dividing the number by 2 and noting the remainders.

Step-by-Step Explanation

1.Read the number from the user.

2.Check validation:

• If the number is less than or equal to 0, stop and show invalid input.

3. Initialize an empty list (or string) to store remainders.

4. While the number is greater than 0:

• Divide the number by 2.

• Store the remainder (number % 2).

• Update the number to the quotient (number / 2).

5.The remainders collected are in reverse order.

6.Reverse the stored remainders.

7.Print the reversed sequence as the binary number.

Input / Output Format

Input Format
A single positive integer entered by the user.


Output Format
The binary equivalent of the number, along with the step-by-step conversion process.
Constraints
• Input must be a positive integer

• Decimal number ≥ 1

Examples

Input:
13
Output:
Binary: 1101

Example Solution (Public)

C
#include <stdio.h>

int main() {
    int num, temp;
    int binary[32];
    int i = 0;

    /* ---- Input & Validation ---- */
    while (1) {
        printf("Enter a positive integer: ");

        if (scanf("%d", &num) != 1) {
            printf("Invalid input! Please enter an integer.n");
            while (getchar() != 'n'); // clear buffer
            continue;
        }

        if (num <= 0) {
            printf("Invalid input! Enter a positive integer only.n");
        } else {
            break;
        }
    }

    /* ---- Conversion Process ---- */
    temp = num;
    printf("nStep-by-step conversion (Decimal to Binary):n");
    printf("QuotienttRemaindern");

    while (temp > 0) {
        binary[i] = temp % 2;
        printf("%dtt%dn", temp / 2, binary[i]);
        temp = temp / 2;
        i++;
    }

    /* ---- Print Binary ---- */
    printf("nBinary equivalent of %d is: ", num);
    for (int j = i - 1; j >= 0; j--) {
        printf("%d", binary[j]);
    }
    printf("n");

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    int num, temp;
    int binary[32];
    int i = 0;

    /* ---- Input & Validation ---- */
    while (1) {
        printf("Enter a positive integer: ");

        if (scanf("%d", &num) != 1) {
            printf("Invalid input! Please enter an integer.n");
            while (getchar() != 'n'); // clear buffer
            continue;
        }

        if (num <= 0) {
            printf("Invalid input! Enter a positive integer only.n");
        } else {
            break;
        }
    }

    /* ---- Conversion Process ---- */
    temp = num;
    printf("nStep-by-step conversion (Decimal to Binary):n");
    printf("QuotienttRemaindern");

    while (temp > 0) {
        binary[i] = temp % 2;
        printf("%dtt%dn", temp / 2, binary[i]);
        temp = temp / 2;
        i++;
    }

    /* ---- Print Binary ---- */
    printf("nBinary equivalent of %d is: ", num);
    for (int j = i - 1; j >= 0; j--) {
        printf("%d", binary[j]);
    }
    printf("n");

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.