porem nao tenho a minima ideia de como fazer pois nao tive aulas sobre binarios e tals pois entrei no segundo semestre algem pode me ajudar pra clarear as minhas ideias
Leia: http://pt.wikihow.com/Converter-N%C3%BAmeros-Bin%C3%A1rios-em-Decimais
Depois recomendo realizar a leitura de uma string contendo o numero em binário, e aplicar o algoritmo acima
Abx
#include <stdio.h> //printf
#include <string.h> //strlen
#include <stdint.h> //uintX_t or use int instead - depend on platform.
/* reverse string */
char *strrev(char *str){
int end = strlen(str)-1;
int start = 0;
while( start<end ){
str[start] ^= str[end];
str[end] ^= str[start];
str[start] ^= str[end];
++start;
--end;
}
return str;
}
/* transform binary string to integer */
uint32_t binstr2int(char *bs){
uint32_t ret = 0;
uint32_t val = 1;
while(*bs){
if (*bs++ == '1') ret = ret + val;
val = val*2;
}
return ret;
}
int main(void){
char binstr[] = "11"; //1428875423
char s[200];
scanf("%s", s);
printf("Binary: %s, Int: %d\n", s, binstr2int(strrev(s)));
system("pause");
return 0;
}
Para responder essa pergunta devemos colocar em prática nosso conhecimento sobre Algoritmo e Estrutura de Dados.
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main()
{
int bin, dec = 0, i;
printf(" Entre um numero binario: ");
scanf("%d", &bin);
for(i = 0; bin > 0; i++)
{
dec = dec + pow(2, i) * (bin % 10);
bin = bin ⁄ 10;
}
printf(" O equivalente do numero em decimal é: %d ", dec);
return 0;
}
Portanto, o algoritmo acima consegue realizar a conversão de qualquer número binário em um número na base decimal, conforme o solicitado.
Para escrever sua resposta aqui, entre ou crie uma conta.
Lógica de Programação e Algoritmos
•FAMA
Compartilhar