Jump to content
Fórum Script Brasil
  • 0

Conversao Ii


balduino
 Share

Question

#include<stdio.h>

#include<stdlib.h>

void main()

{

long int dec,n=0,total_e,i=0;

int temp[20],bin[20];

printf("numero decimal: ");

scanf("%ld",&dec);

fflush(stdin);

if(dec<0)

return;

if(dec==0)

{

printf("numero binario: 0\n");

system("pause");

return;

}

printf("numero binario: ");

while(dec>0)

{

temp[i++]=dec%2;

dec=dec/2;

}

total_e=i;

while(n<total_e)

{

bin[n]=temp[--i];

printf("%d",bin[n]);

n++;

}

printf("\n");

system("pause");

}

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Em C++:

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

string dec_to_binary(long c) {
    string result;
    do result.insert(result.begin(), (c%2) + '0'); while((c = c>>1) >= 1);
    return result;
}

long binary_to_dec(string bin) {
    long dec = 0;
    for(int i = 0; i < bin.length(); i++) {
        dec *= 2;
        if(bin[i] - '0') dec++;
    }
    return dec;
}

int main() {
    cout << dec_to_binary(10) << endl;
    cout << binary_to_dec("1010") << endl;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Forum Statistics

    • Total Topics
      149.8k
    • Total Posts
      646.6k
×
×
  • Create New...