Ad Code

Programs to Convert Binary to Decimal and Decimal to Binary in C++, C, and Java.

Welcome to Codingfizz,


Converting between binary and decimal numbers is a common task in computer science. Whether you're working with low-level hardware, encoding data, or performing arithmetic, understanding how to convert between these two number systems is an essential skill. Fortunately, there are simple algorithms that can be used to convert binary to decimal and vice versa. In this post, we'll explore how to implement these algorithms in three popular programming languages: C++, C, and Java.

Program in C to convert a binary number to decimal:

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

int binaryToDecimal(long long binaryNumber) {
    int decimalNumber = 0, i = 0, remainder;
    while (binaryNumber != 0) {
        remainder = binaryNumber % 10;
        binaryNumber /= 10;
        decimalNumber += remainder * pow(2, i);
        ++i;
    }
    return decimalNumber;
}

int main() {
    long long binaryNumber;
    printf("Enter a binary number: ");
    scanf("%lld", &binaryNumber);
    printf("Decimal number: %d", binaryToDecimal(binaryNumber));
    return 0;
}

Output:

Enter a binary number: 101010

Decimal number: 42

Program in C to convert a decimal to a binary number:
#include <stdio.h>
#include <stdio.h>

long long decimalToBinary(int decimalNumber) {
    long long binaryNumber = 0;
    int remainder, i = 1;
    while (decimalNumber != 0) {
        remainder = decimalNumber % 2;
        decimalNumber /= 2;
        binaryNumber += remainder * i;
        i *= 10;
    }
    return binaryNumber;
}

int main() {
    int decimalNumber;
    printf("Enter a decimal number: ");
    scanf("%d", &decimalNumber);
    printf("Binary number: %lld", decimalToBinary(decimalNumber));
    return 0;
}

Output:

Enter a decimal number: 255

Binary number: 11111111

Program in C++ to convert a binary number to decimal:

#include <iostream>
#include <cmath>
using namespace std;

int binaryToDecimal(long long binaryNumber) {
    int decimalNumber = 0, i = 0, remainder;
    while (binaryNumber != 0) {
        remainder = binaryNumber % 10;
        binaryNumber /= 10;
        decimalNumber += remainder * pow(2, i);
        ++i;
    }
    return decimalNumber;
}

int main() {
    long long binaryNumber;
    cout << "Enter a binary number: ";
    cin >> binaryNumber;
    cout << "Decimal number: " << binaryToDecimal(binaryNumber);
    return 0;
}

Output:

Enter a binary number: 101010

Decimal number: 42

Program in C++ to convert a decimal to a binary number:
#include <iostream>
#include <cmath>
using namespace std;

long long decimalToBinary(int decimalNumber) {
    long long binaryNumber = 0;
    int remainder, i = 1;
    while (decimalNumber != 0) {
        remainder = decimalNumber % 2;
        decimalNumber /= 2;
        binaryNumber += remainder * i;
        i *= 10;
    }
    return binaryNumber;
}

int main() {
    int decimalNumber;
    cout << "Enter a decimal number: ";
    cin >> decimalNumber;
    cout << "Binary number: " << decimalToBinary(decimalNumber);
    return 0;
}

Output:

Enter a decimal number: 255

Binary number: 11111111

Program in Java to convert a binary number to decimal:

import java.util.Scanner;

public class BinaryToDecimal {
    public static int binaryToDecimal(long binaryNumber) {
        int decimalNumber = 0, i = 0;
        long remainder;
        while (binaryNumber != 0) {
            remainder = binaryNumber % 10;
            binaryNumber /= 10;
            decimalNumber += remainder * Math.pow(2, i);
            ++i;
        }
        return decimalNumber;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a binary number: ");
        long binaryNumber = scanner.nextLong();
        System.out.println("Decimal number: " + binaryToDecimal(binaryNumber));
    }
}

Output:

Enter a binary number: 101010

Decimal number: 42

Program in Java to convert a decimal to a binary number:
import java.util.Scanner;

public class DecimalToBinary {
    public static long decimalToBinary(int decimalNumber) {
        long binaryNumber = 0;
        int remainder, i = 1;
        while (decimalNumber != 0) {
            remainder = decimalNumber % 2;
            decimalNumber /= 2;
            binaryNumber += remainder * i;
            i *= 10;
        }
        return binaryNumber;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int decimalNumber = scanner.nextInt();
        System.out.println("Binary number: " + decimalToBinary(decimalNumber));
    }
}

Output:

Enter a decimal number: 255

Binary number: 11111111

In this post, we've looked at example programs in C++, C, and Java that can help you perform these conversions. By understanding how these programs work, you can gain a deeper understanding of how binary and decimal numbers are represented and manipulated in software. With this knowledge, you'll be better equipped to tackle more complex programming challenges in the future.

Post a Comment

0 Comments

Ad Code