Thursday, July 4, 2019

convert binary octal to decimal in c language

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

int main()
{
    int num,base,result;
    char choice;
    printf("enter b for binary and o for octal\n");\
    scanf("%c",&choice);
    printf("enter the number\n");
    scanf("%d",&num);

  if(choice=='b')
  {
    base=2;

  }
  else
    base=8;
 //result=fun(num,base);
 printf("Decimal number is %d\n",fun(num,base));
    return 0;
}

fun(int n,int base)
{
    int rem,d,j=1,dec=0;
    while(n>0)
    {
        rem=n%10; /* taking last digit */
        d=rem*j;
        dec+=d;
        j*=base;
        n/=10; /* skipping last digit */
    }

return dec;
}

No comments:

Post a Comment

python class topic video