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[] = {1, 0, 1, 0};int decimal = 0;int len = sizeof(binary) / sizeof(binary[0]);for (int i = 0; i < len; i++){decimal = decimal + binary[i] * pow(2, len - 1 - i);}printf("Decimal number is %d", decimal);return 0;}
Java
public class DecimalToBinary {public static void main(String[] args) {int binary[] = { 1, 0, 1, 0 };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
- Write a program to convert Binary to octal
- Write a program to convert Binary to Hexadecimal
- Write a program to convert Binary to Decimal
- Write a program to convert Octal to Binary
- Write a program to convert Octal to Hexadecimal
- Write a program to convert Octal to Decimal
- Write a program to convert Hexadecimal to Binary
- Write a program to convert Hexadecimal to Octal
- Write a program to convert Hexadecimal to Decimal
- Write a program to convert Decimal to Binary
- Write a program to convert Decimal to Octal
- Write a program to convert Decimal to Hexadecimal
No comments:
Post a Comment