c funvtion to convert decimal in octal with returning array pointer

nzvtunqtr
timer Asked: Jul 5th, 2015

Question Description

  #include <stdio.h>

#include<string.h>

#include<stdlib.h>

char* findBase(int , int );

int main()

  int n,d;

  char *b,*h,*o;

  do

  {

    printf("\n1.Decimal to Binary\n2.Decimal to Octal\n3.Decimal to Hexadecimal \n4.Exit\nEnter your choice:");

    scanf("%d",&n);

   

    switch(n)

    {

      case 1: 

       printf("\nEnter the decimal value:");

    scanf("%d",&d);

     b=findBase(d,2);

      printf("\nEquivalent Binary value of %d is %s",d,b);

        break;

      case 2:

       printf("\nEnter the decimal value:");

    scanf("%d",&d);

     o= findBase(d,8);

    printf("\nEquivalent  Octal value of %d is %s",d,o);

        break;

      case 3:

       printf("\nEnter the decimal value:");

    scanf("%d",&d);

     h= findBase(d,16);

      printf("\nEquivalent  Hexadecimal value of %d is %s",d,h);

      break;

        case 4: return 0;

    };

  }

  while(n<4);

  return 0;

}

char* findBase(int n, int b )

{

 long int quotient;

int i=1,j,temp,k=0;

static char ans[100],ret[100];

quotient =n;

  if(b==16){

    i=1;

    while(quotient!=0) {

temp = quotient % 16;

//To convert integer into character

if( temp < 10)

          temp =temp + 48; else

        temp = temp + 55;

ans[i++]= temp;

quotient = quotient / 16;

}

//printf("\nEquivalent  Hexadecimal value of %d is ",n);

for (j = i -1,k=0 ;j> 0;j--,k++)

     ret[k]=ans[j];

}

if(b==8)

{

  i=1;

  while(quotient!=0){

    temp=quotient % 8;

    temp=temp+48;

      ans[i++]= temp;

      quotient = quotient / 8;

  }

  //printf("\nEquivalent  Octal value of %d is ",n);

  for(j = i -1,k=0 ;j> 0;j--,k++)

      ret[k]=ans[j];

}

if(b==2)

{

  i=1;

  while(quotient!=0){

    temp=quotient % 2;

    if(temp==0)

      ans[i++]='0';

    else

      ans[i++]='1';

      quotient = quotient / 2;

  }

  //printf("\nEquivalent  Binary value of %d is ",n);

  for(j = i -1,k=0 ;j> 0;j--,k++)

      ret[k]=ans[j];

}

  ptr=ret;

  return ptr; 

}

here for decimal 16 binary will be 10000

and for decimal 16 octal should be 20

but octal is showing 2000

if input given after binary

if octal input is given first comes 20


User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

This question has not been answered.

Create a free account to get help with this and any other question!

Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors