Lets write a C program to convert a number from Octal number system(base 8) to Decimal number system(base 10), using while loop.
Page Contents
Number Systems
1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.
Related Read:
while loop in C programming
Calculate Sum of Digits: C Program
C Program To Reverse a Number
Note: Octal number system can be derived by base 8 to the power of whole numbers.
Example:
etc .. 84, 83, 82, 81, 80
etc .. 84 = 4,096, 83 = 512, 82 = 64, 81 = 8, 80 = 1.
Expected Output for the Input
User Input:
Enter an Octal Number
24
Output:
Decimal Equivalent of 24 is 20.
Explanation:
If user enters num = 24.
(83 x 0) + (82 x 0) + (81 x 2) + (80 x 4 )
= 0 + 0 + (8 x 2) + (1 x 4)
= 16 + 4
= 20
So Decimal equivalent of Octal number 24 is 20.
Video Tutorial: C Program To Convert Octal Number To Decimal Number, using While Loop
[youtube https://www.youtube.com/watch?v=QVksCjl2yhY]
Logic To Convert Octal Number To Decimal Number, using While Loop
In this C program we ask the user to enter / input an Octal number. Using while loop we calculate the reminder and add it to previous value of variable dec, along with pow(8, place). We make use of variable place to position the reminder.
dec = dec + rem * pow(8, place);
Source Code: C Program To Convert Octal Number To Decimal Number, using While Loop
#include < stdio.h > #include < math.h > int main() { int num, dec = 0, rem = 0, place = 0; printf("Enter an Octal Number\n"); scanf("%d", &num); printf("\nDecimal Equivalent of %d is ", num); while(num) { rem = num % 10; dec = dec + rem * pow(8, place); num = num / 10; place++; } printf("%d\n", dec); return 0; }
Output 1:
Enter a decimal number
16
Octal Equivalent of 16 is 20
Output 2:
Enter an Octal Number
24
Decimal Equivalent of 24 is 20
Output 3:
Enter an Octal Number
132
Decimal Equivalent of 132 is 90
Output 4:
Enter an Octal Number
16
Decimal Equivalent of 16 is 14
Output 5:
Enter an Octal Number
40
Decimal Equivalent of 40 is 32
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert
This was really amazing was of explanation!! you’re doing really great job.
Thanks for this much of an effort
Thanks a lot for your kind words. I’ll try my best to keep providing maximum value 🙏