Convert binary number to decimal number

Write a program to convert binary number to decimal number

Example:

Input:  binary[]={1,0,1,0}
Output: Decimal number is 10

Approach

C

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

int main()
{
    int binary[] = {1010};
    int decimal = 0;
    int len = sizeof(binary) / sizeof(binary[0]);
    for (int i = 0i < leni++)
    {
        decimal = decimal + binary[i] * pow(2len - 1 - i);
    }
    printf("Decimal number is %d"decimal);
    return 0;
}

Java


public class DecimalToBinary {
    public static void main(String[] args) {
        int binary[] = { 1010 };
        int decimal = 0;
        int len = binary.length;
        for (int i = 0; i < len; i++) {
            decimal = (int) (decimal + binary[i] * Math.pow(2, len - 1 - i));
        }
        System.out.printf("Decimal number is %d", decimal);
    }

}


Related posts

  1. Write a program to convert Binary to octal
  2. Write a program to convert Binary to Hexadecimal
  3. Write a program to convert Binary to Decimal
  4. Write a program to convert Octal to Binary
  5. Write a program to convert Octal to Hexadecimal
  6. Write a program to convert Octal to Decimal
  7. Write a program to convert Hexadecimal to Binary
  8. Write a program to convert Hexadecimal to Octal
  9. Write a program to convert Hexadecimal to Decimal
  10. Write a program to convert Decimal to Binary
  11. Write a program to convert Decimal to Octal
  12. Write a program to convert Decimal to Hexadecimal


No comments:

Post a Comment