Pages

This blog is under construction

Thursday, December 20, 2018

C program to convert octal number to decimal number.

Example  (125)8  =(?)10

Solution:


=125 
=1X82 +2X81 + 5X80  
=64 + 16 + 5
=85

C Program:

#include<stdio.h>
#include<math.h>
void main()
{ int n,r,s=0,p=0;
   printf("\nEnter an octal no.");
   scanf("%d",&n);
   while(n>0)
   {
     r=n%10;
     if(r<0 || r>7)
     {
       printf("\nInput no is not octal");
       exit(0);
    }
  s=s+r*pow(8,p);
  p++;
  n=n/10;
 }
printf("\nDecimal no. equivalent to input octal no.is:=%d",s);

}

Output:

Enter an octal no.125                                                                                                                                
                                                                                                                                                     
Decimal no. equivalent to input octal no.is:=85 


No comments:

Post a Comment