Learn about Decimal to Octal conversion in C using Function in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Decimal to Octal conversion in C using Function
Program:
#include<stdio.h>
#include<math.h>
void convert(int n);
int main()
{
int decimal;
printf("Enter Decimal Number: ");
scanf("%d",&decimal);
convert(decimal);
return 0;
}
void convert(int n)
{
int oct=0, rem, i=0;
while(n!=0)
{
rem = n%8;
oct += (rem*pow(10,i));
n /= 8;
i++;
}
printf("Octal Value= %d",oct);
}
Output:
Enter Decimal Number: 48
Octal Value= 60
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C program to convert decimal to octal using while loop
C program to convert binary to decimal using function