C Program to Convert Octal to Decimal using loops

0

Method 1 – Using While Loop

#include<stdio.h>
 
#include<math.h>
  
int main()
{
      int decimal_number = 0, remainder, octal_number;
       
      int count;
       
      printf("Enter an Octal Number:\t");
       
      scanf("%d", &octal_number); 
       
      for(count = 0; octal_number > 0; count++)
      {
       
            remainder = octal_number % 10;
       
            decimal_number = decimal_number + remainder * pow(8, count);
       
            octal_number = octal_number / 10;
      }
       
      printf("\nDecimal Equivalent:\t%d\n", decimal_number);
       
      return 0;
}

Method 2 – Using For Loop

#include <stdio.h>
 
#include <math.h>
 
int main()
 
{
 
    long int octal, decimal = 0;
 
    int i = 0;
 
    printf("Enter any octal number: ");
 
    scanf("%ld", &octal);
 
    while (octal != 0)
 
    {
 
        decimal =  decimal +(octal % 10)* pow(8, i++);
 
        octal = octal / 10;
 
    }
 
    printf("Equivalent decimal value: %ld",decimal);
 
    return 0;
 
}

Output

Enter any octal number: 67
Equivalent decimal value: 55

Leave a Reply

Your email address will not be published. Required fields are marked *