C Exercises: Convert Octal number to a Decimal number
C For Loop: Exercise-51 with Solution
Write a C program to convert an octal number to a decimal without using an array.
Visual Presentation:
Sample Solution:
C Code:
#include <stdio.h>
int main() {
int n1, n5, p = 1, k, ch = 1;
int dec = 0, i = 1, d;
printf("\n\nConvert Octal to Decimal:\n ");
printf("-------------------------\n");
printf("Input an octal number (using digits 0 - 7): ");
scanf("%d", &n1);
n5 = n1;
// Checking if any digit in the input number is greater than or equal to 8
for (; n1 > 0; n1 = n1 / 10) {
k = n1 % 10;
if (k >= 8) {
ch = 0;
break; // No need to continue checking if one invalid digit is found
}
}
// Switch-case to determine if the input is a valid octal number or not
switch (ch) {
case 0:
printf("\nThe number is not an octal number.\n\n");
break;
case 1:
n1 = n5;
// Calculating the decimal equivalent of the octal number
for (; n1 > 0; n1 = n1 / 10) {
d = n1 % 10;
dec += d * p;
p *= 8;
}
// Displaying the input octal number and its equivalent decimal number
printf("\nThe Octal Number: %d\nThe equivalent Decimal Number: %d\n\n", n5, dec);
break;
}
return 0;
}
Sample Output:
Convert Octal to Decimal: ------------------------- Input an octal number (using digit 0 - 7) :745 The Octal Number : 745 The equivalent Decimal Number : 485
Explanation:
Here's a brief explanation.
- Header and Declaration:
- Includes the necessary header file (stdio.h).
- Declare variables 'n1' (input octal number), 'n5' (backup of the input), 'p' (power of 8 multiplier), 'k' (temporary variable for digit checking), 'ch' (flag for octal validity check), 'dec' (resulting decimal number), 'i' (loop variable), and 'd' (temporary variable for digit extraction).
- User Input:
- Asks the user to input an octal number (using digits 0 - 7).
- Reads the input and stores it in the variable 'n1'.
- Octal Validity Check:
- Uses a for loop to iterate through each digit of the input octal number ('n1').
- Checks if any digit is greater than or equal to 8, marking the number as invalid if true.
- Switch-Case for Octal Validity:
- Uses a switch statement based on the validity check ('ch').
- If invalid, prints a message indicating that the input is not an octal number.
- If valid, calculate the decimal equivalent.
- Decimal Equivalent Calculation:
- Reset 'n1' to the backup ('n5').
- Use a for loop to extract each digit of the octal number, multiplying it by the corresponding power of 8 ('p') and accumulating the result in the 'dec' variable.
- Result Display:
- Prints the input octal number and its equivalent decimal number.
- Return Statement:
- Returns 0 to indicate successful execution.
Flowchart:
C Programming Code Editor:
Previous: Write a program in C to convert a decimal number into octal without using an array.
Next: Write a program in c to find the Sum of GP series.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics