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

Raju is a Civil Engineer. He is writing software to automate his work. As a part of his requirement, he wants to calculate the circle diameter, circumference, and area. Help Raju to complete his task. Get radius as input.

OUTPUT

Implement structures to read, write and compute average- marks and the students scoring above and below the average marks for a class of N students Program and Algorithm Computer Science Engineering VTU

Structures abov below avg #include<stdio.h> struct student { char name [100]; char usn [20]; float marks; }; void main() { int i,j,k,m,n,aboveavg[100],belowavg[100]; float total=0.0,avg=0.0; struct student s[100]; printf("enter number of students\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the details of students%d\n",i+1); printf("enter name\n"); scanf("%s",s[i].name); printf("enter usn\n"); scanf("%s",s[i].usn); printf("enter marks\n"); scanf("%f",&s[i].marks); total+=s[i].marks; } printf("enter the details of students\n"); printf("name\t\t usn\t\t marks\n"); for(i=0;i<n;i++) printf("%s\t\t %s\t\t %f\n",s[i].name,s[i].usn,s[i].marks); avg=total/n; printf("avg=%f\n",avg); j=0; k=0; for(i=0;i<n;i++) { if(s[i].marks>avg) abov...