Wednesday, November 2, 2011

Decimal to Octal

Q. Write a program in C to convert a decimal number to its octal equivalent.

Ans.
Coding 4 decimal to octal#include<stdio.h>
#include<conio.h>
void convert(int num, int rem);
int main()
{
 int n,m;
 printf("Enter number to change in octal: ");
 scanf("%d",&n);
 convert(n,m);
 return 0;
}
void convert(int num, int rem)
{
 if(rem==0)
    return;
 rem=num%8;
 num=num/8;
 convert(num,rem);
 if(rem!=0)
    printf("%d",rem);
}
/******************Output************
Enter number to change in octal : 786
1422

Hence, (786)10=(1422)8
**********************************/

No comments:

Post a Comment