C program to convert Decimal to Octal
In this post, you will learn how to write a C program to convert the given decimal number into an equivalent octal number. It means how we can convert the number with base value 10 to base value 8 using the C programming language.
A decimal number is a base-10 number system. It ranges from 0 to 9, i.e., 0,1,2,3,4,5,6,7,8,9. Any combination of digits is a decimal number, such as 61, 22, 912, 0, 5 etc. The main advantages of the decimal number system are that it is easy-to-read, used by humans, and easy to manipulate. Here, we have mentioned two ways to convert decimal to Octal.
The octal number system has a base of eight, like 238, 1028, 418, etc., and uses the numbers from 0 to 7. The octal number system is widely used in computer application sectors and digital numbering systems. The main advantage of using Octal numbers is that it uses less digits than the decimal and hexadecimal number systems. That's why it has fewer computations and less computational errors.
Simple C program to convert Decimal to Octal
In the given C program, we have taken decimal input from the user and converted it to octal using a simple, straightforward type conversion process.
#include <stdio.h>
int main()
{
int num;
printf("\nPlease enter the number : ");
scanf("%d", &num);
printf("\nOctal value of the given number : %o", num);
return 0;
}
Output 1
Please enter the number : 33
Octal value of the given number : 41
Output 2
Please enter the number : 23
Octal value of the given number : 27
Convert decimal to octal in C using for loop
Here, we have used the for loop to convert user-defined decimal to an octal number system using the C program.
/* C Program to Convert Decimal to Octal Number */
#include <stdio.h>
int main()
{
int octal_num[10], num, i, j;
printf("\nPlease enter the number : ");
scanf("%d", &num);
for(i = 0; num > 0; i++)
{
octal_num[i] = num % 8;
num = num / 8;
}
printf("\nOctal value of the given number : ");
for(j = i - 1; j >= 0; j--)
{
printf("%d", octal_num[j]);
}
return 0;
}
Output 1
Please enter the number : 21
Octal value of the given number : 25
Output 2
Please enter the number : 32
Octal value of the given number : 40
Convert decimal to octal in C using while loop
In the given C program, we have used the while loop to convert decimal to octal number. First, we take a decimal number by user input and divide the input number by 8 to obtain its remainder and quotient. We store the remainder in an array octalnum. We repeat this process until the quotient becomes zero. When it becomes zero, print the array octalnum in the reverse order to get the output.
#include <stdio.h>
int main()
{
long decimalnum, remainder, quotient;
int octalnum[100], i = 1, j;
printf("Enter the decimal number: ");
scanf("%ld", &decimalnum);
quotient = decimalnum;
while (quotient != 0)
{
octalnum[i++] = quotient % 8;
quotient = quotient / 8;
}
printf("Equivalent Octal Number %d: ", decimalnum);
for (j = i - 1; j > 0; j--)
printf("%d", octalnum[j]);
return 0;
}
Output of the above code:
Enter the decimal number: 87
Equivalent Octal Number 87: 127
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative