Tuesday, June 4, 2019

Write a program to convert binary number to decimal number

/* ***********************************************
    Write a program to convert binary number to decimal number

 date:04-06-2019
 created by:ANIL DURGAM

*****************************************************/


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,nsave,rem,d,j=1,dec=0;
    printf("Enter the binary number\n");
    scanf("%d",&n);
    nsave=n;
    while(n>0)
    {
        rem=n%10;
        d=rem*j;
        dec+=d;
        j*=2;
        n/=10;
    }
    printf("binary num==%d,Decimal number=%d\n",nsave,dec);

    return 0;
}

















program 2:

#include <stdio.h>
#include <stdlib.h>

long binary (long int num);
main()
{
    long int num;
    printf("Enter the decimal number\n");
    scanf("%ld",&num) ;
    printf("Decimal=%ld,Binary=%ld\n",num,binary(num));
}
long binary(long int num)
{
long rem,a=1,bin=0;
while(num>0)
{
    rem=num%2;
    bin=bin+rem*a;
    num/=2;
    a*=10;
}
    return bin;
}

No comments:

Post a Comment

python class topic video