Sunday 1 September 2013

Write a C code for decimal to octal converter



Write a C code for decimal to octal converter

#include<stdio.h>
int main(){

  long int decimalNumber,remainder,quotient;
  int octalNumber[100],i=1,j;

  printf("Enter any decimal number: ");
  scanf("%ld",&decimalNumber);

  quotient = decimalNumber;

  while(quotient!=0){
      octalNumber[i++]= quotient % 8;
      quotient = quotient / 8;
  }

  printf("Equivalent octal value of decimal number %d: ",decimalNumber);
  for(j = i -1 ;j> 0;j--)
      printf("%d",octalNumber[j]);

  return 0;
}

Sample output:
Enter any decimal number: 50
Equivalent octal value of decimal number 50: 62

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved