This program converts either octal number entered by user to decimal number or decimal number entered by user to octal in accordance with the character entered by user.

## Source Code to Convert Octal Number to Decimal and Vice Versa

```
/* C programming source code to convert either octal to decimal or decimal to octal according to data entered by user. */
#include <stdio.h>
#include <math.h>
int decimal_octal(int n);
int octal_deciaml(int n);
int main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'o' to convert decimal to octal.\n");
printf("2. Enter alphabet 'd' to convert octal to decimal.\n");
scanf("%c",&c);
if (c =='d' || c == 'D')
{
printf("Enter an octal number: ");
scanf("%d", &n);
printf("%d in octal = %d in decimal", n, octal_decimal(n));
}
if (c =='o' || c == 'O')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in octal", n, decimal_octal(n));
}
return 0;
}
int decimal_octal(int n) /* Function to convert decimal to octal */
{
int rem, i=1, octal=0;
while (n!=0)
{
rem=n%8;
n/=8;
octal+=rem*i;
i*=10;
}
return octal;
}
int octal_decimal(int n) /* Function to convert octal to decimal */
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(8,i);
++i;
}
return decimal;
}
```

**Output**

`Instructions:
1. Enter alphabet 'o' to convert decimal to octal.
2. Enter alphabet 'd' to convert octal to decimal.
d
Enter an octal number: 2341
2341 in octal = 1249 in decimal
`

This program asks user to enter a character and in accordance with that character user is asked to enter either octal number or decimal number. If user chooses to convert octal number to decimal then, that number is passed to function `octal_decimal()`

. This function will convert the octal number passed by user to decimal number and returns it to main function. Similarly, if user chooses to convert decimal number to octal then, that number is passed to function `decimal_octal()`

. This function will convert decimal number to octal number and returns it to main function.

### Displaying and Reading Octal Number using %o

In the above program, it is shown how you can convert decimal number to octal and octal to decimal number manually. But, in C programming, there is easy solution for it. You can display the corresponding octal number of a decimal number directly using %o format string. You also can take number from user in octal form using %o. This program will demonstrate the working of %o.

```
/* C program to take and display number in octal form */
#include <stdio.h>
int main()
{
int n;
printf("Enter a decimal number: ");
scanf("%d",&n);
/* %o will display the integer in corresponding octal form */
printf("%d in decimal = %o in octal", n, n);
printf("\nEnter an octal number: ");
scanf("%o",&n); /* Takes number in octal form.*/
printf("%o in octol = %d in decimal", n, n);
return 0;
}
```