C program to convert decimal to Octal number

C program to convert decimal to Octal number

This page has a C program to convert decimal to Octal number . We can write the C program to convert the given mixed decimal number into its equivalent Octal. For Integer part conversion to Octal divide the given integer part of the given decimal number by 8 again and again until the Integer part of the decimal number can be divided by 8 and print the remainders for each division (form bottom to top) to form the Octal equivalent. For decimal part conversion, multiply the given decimal part (number after the decimal point) of the decimal number by 8 again and again for atleast 5 or 7 steps(times) and print the carries for each multiplication (form top to bottom) to form the Octal equivalent.  The below given C program to convert a given mixed decimal number into its equivalent Octal numberKindly check out the program to convert decimal to Octal number.

Decimal to Octal number Conversion 


   #include<stdio.h>
   #include<string.h>
   void octalinteger(long int);
   void octalfractional(long int,int);
   int power(int,int);
   int main(){ int a=0,j=0,m,n=0,q=1;
           long int k1,k2;
           char g1[80], p1[80]={'\0'}, p2[80]={'\0'};
           clrscr();
           printf("Give a decimal number : ");
           gets(g1);
           for (a = 0; g1[a] != '\0'; a++)
           {
           q++;
           }
           for(m=0;m<q-1;m++){
           if(g1[m] == '.')
           break;
           p1[m]=g1[m];
           }
           for(a=m+1;a<=q-1;a++){
           p2[n]=g1[a];
           n++;
           j++;
           }
           k1= atoi(p1);
           k2= atoi(p2);
           printf("\n\n The given decimal number is : %s \n\n", g1);
           octalinteger(k1);
           for(m=0;m<q-1;m++){
           if(g1[m] == '.'){
           printf(".");
           break;
           }
           }
           if(g1[m] == '.')
           octalfractional(k2, j-1);
           getch();
           return(0);
           }

    void octalinteger(long int s){
           int b=1, h, remai[40];
           printf("\n\n The Octal number is : ");
           while(s>0){
           remai[b] = s % 8;
           s = s / 8;
           b++;
           }
           b--;
           for(h=b;h>=1;h--)
           printf("%d", remai[h]);
           return(0);
           }

   void octalfractional(long int e, int u){
           int a,r;
           int p;
           for(a=1;a<=8;++a){
           e=e*8;
           p=power(10,u);
           if(e>=p){
           r = e / p;
           e = e % p;
           printf("%d",r);
           }
           else
           printf("%d",0);
           }
           return(0);
           }

   int power(int x,int n){
            int i;
            int num = 1;
            for (i = 0; i < n; ++i)
            num *= x;
            return(num);
            }


Sample Output:

Give a decimal number : 486.176

The given decimal number is : 486.176

The Octal number is :  746.13207126

 


About Er. T.Tharesh Kumar

I am a Professor and Head of Department of Computer Science in an Arts and Science College. I have designed this website to provide solved example programs of different categories for all the school and college going students to learn C language deeply, and to practice Interview based Multiple Choice (Objective Type) questions and answers.

Other Related C Programs :


* C Program to find whether the given number is Odd or Even

* C Program to find the Sum of Odd Digits

* C Program to find the Sum of Even Digits

* C Program to find the Sum of Even and Odd Digits,

* C Program to Display Odd digits

* C Program to Display even digits

* C Program to Display Odd Numbers,

* C Program to Display Even Numbers,

* C Program to Check whether the given number is Armstrong number

* C Program to display the Armstrong numbers in Range

* C Program to display the Armstrong numbers between 1 and 1000

* C Program to check perfect number, perfect numbers in range

* C Program to Check Prime number, Prime numbers between 1 and 100

* C Program to display the Prime numbers in a Range

* C Program to display the Sum of Prime numbers

* C Program to Check Strong number

* C Program to display the Strong number in Range

* C Program to Check whether the given number is polindrome number or not

* C Program to Check polindrome number using for loop

* C Program to Check polindrome number using Recursion

* C Program to  check whether the given string is polindrome or not

* C Program to find the Roots of Quadratic equation

* C Program to display Fibonacci Series

* C Program to display the Fibonacci Series using an array

* C Program to display the Fibonacci Series using while loop

* C Program to find the Sum of Fibonacci Series, Factorial of Number

* C Program to find the Factorial without using Recursion

* C Program to find the Factorial using Pointers

* C Program to find the Factorial using Function

Bookmark and Share