Octal to decimal conversion in c

C code to convert octal number to decimal number

#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;
}

Sample output:

Enter any octal number: 346
Equivalent decimal value: 230

C program to change octal to decimal

#include<stdio.h>

int main(){
   
    long int octalNumber;

    printf("Enter any octal number: ");
    scanf("%o",&octalNumber);

    printf("Equivalent decimal number is: %d",octalNumber);

    return 0;
}

Sample output:

Enter any octal number: 17
Equivalent decimal number is: 15

No comments:

Post a Comment