2. Write A C Program Using Functions To Convert Binary Number To Decimal And Vice-versa Code Example


Example 1: built in function in c++ for binary to decimal

#include <bits/stdc++.h>  using namespace std;  int main(void){     bitset<8> bits("1000");     int ab = bits.to_ulong();     cout << ab << "\n";          return 0; }

Example 2: binary to decimal in c

#include <math.h> #include <stdio.h> int convert(long long n); int main() {     long long n;     printf("Enter a binary number: ");     scanf("%lld", &n);     printf("%lld in binary = %d in decimal", n, convert(n));     return 0; }  int convert(long long n) {     int dec = 0, i = 0, rem;     while (n != 0) {         rem = n % 10;         n /= 10;         dec += rem * pow(2, i);         ++i;     }     return dec; }

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable