Skip to main content

Binary to Decimal conversion77

Binary to Decimal conversion 
#include<stdio.h>

#include<stdlib.h>

int dec=0;

int bintodec(int num,int x,int base);

void main()

{

int num,n,r,x=0,base=1;

printf("enter a binary number\n");

scanf("%d",&n);

num=n;

while(n>0)

{

r=n%10;

if(r>1)

{

printf("enter a proper binary number\n");

exit(0);

}

x++;

n=n/10;

}

bintodec(num,x,base);

printf("decimal value of %d is %d\n",num,dec);

}

int bintodec (int num,int x,int base)

{

if(x==0)

return 0;

else

dec=dec+(num%10)*base;

bintodec(num/10,x-1,base*2);

}


Algorithm Binary to Decimal conversion
Algorithm : Binary to Decimal conversion

Start

Declare the required variables

Read binary number as n

If n has digits other than 0 and 1 display error message and stop

x = length(n)

base = 1

decimalnumber = binaryToDecimal(n,x,base)

Display decimalnumber

Stop



Algorithm : binaryToDecimal

check if x = 0 then return 0

decimal = decimal + (n % 10 * 2)

binaryToDecimal(n/10,x-1,base*2)

Comments

Popular posts from this blog

Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques Program and Algorithm Computer Science Engineering VTU

STRINGconcatenation #include<stdio.h> #include<stdlib.h> int length (char str[]); int compare (char str1[],char str2[]); void concatenate (char str1[],char str2[]); void main() { char str1[30],str2[30]; int choice,a,i,j; printf("enter 1-string comparision\n"); printf("enter 2-string length\n"); printf("enter 3-string concatenation\n"); printf("enter 4-exit\n"); scanf("%d",&choice); switch(choice) { case 1:printf("enter string 1\n"); scanf("%s",str1); printf("enter string 2\n"); scanf("%s",str2); a=compare(str1,str2); if(a==0) { printf("%s and%s are identical\n",str1,str2); } else { printf("%s and%s are not identical\n",str1,str2); } break; ...

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are quarantined to prevent the spread of bird flu.

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are  quarantined  to prevent the spread of bird flu. immunized butchered secluded CORRECT mingled