- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Check Reverse equal Original
- C Add Subtract Multiply Divide
- C Make Calculator
- C Add Digits of Number
- C Calculate Average, Percentage
- C Calculate Arithmetic Mean
- C Calculate Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Add n Numbers
- C Interchange Numbers
- C Reverse a Number
- C Swap Two Numbers
- C Count Positive Negative Zero
- C Find Largest of Two Numbers
- C Find Largest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Area & Perimeter of Square
- C Area & Perimeter of Rectangle
- C Area & Circumference of Circle
- C Convert Fahrenheit to Celsius
- C Convert Celsius to Fahrenheit
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Generate Armstrong Numbers
- C Find nCr and nPr
- C Convert Decimal to Binary
- C Convert Decimal to Octal
- C Convert Decimal to Hexadecimal
- C Convert Binary to Decimal
- C Convert Binary to Octal
- C Convert Binary to Hexadecimal
- C Convert Octal to Decimal
- C Convert Octal to Binary
- C Convert Octal to Hexadecimal
- C Convert Hexadecimal to Decimal
- C Convert Hexadecimal to Binary
- C Convert Hexadecimal to Octal
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Print Smiling face
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Add Two Numbers using Pointer
- C Find Largest Element in Array
- C Find Smallest Element in Array
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C 3D Array Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Delete Vowels from String
- C Delete Word from String
- C Find Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Generate Random Numbers
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C List Files in Directory
- C Delete a File
- C Encrypt & Decrypt a File
- C Print Date
- C Get IP Address
- C Shutdown Computer
- C More Programs
- C Check Palindrome String
- C Inches to Centimetres
- C Kilogram to Gram
- C Count Even Odd
- C Count Vowels Consonants
- C Find Profit Loss
- C Sum All Matrix Elements
- C First & Last Digit Sum
- C Largest Element in Matrix
- C Pass Array to Function
- C Address of Variable
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Reverse File
- C Print Message with Time
- C Print Number in Words
- C Print Successive Character
- C Sum of their Square
- C Replace Vowel in String
- C Sort Words in Ascending
- C Sort Words in Descending
- C Sort String in Alphabetical
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

« Previous Program Next Program »

In this tutorial, we will learn about how to create a program in C that converts any given decimal number (by user at run-time) to its equivalent octal number. At last we have also created a user-defined function program that does the same job.

Before going through the program, if you are not aware of

- Decimal Number
- Octal Number
- Decimal to Octal Conversion

then refer to Decimal to Octal conversion step by step process. Now let's move on to the program.

To convert decimal number to octal number in C programming, you have to ask from user to enter the decimal number' and convert it into octal number and then display the equivalent value in octal as output like here in the program given below:

// Decimal to Octal in C // ---codescracker.com--- #include<stdio.h> #include<conio.h> int main() { int decnum, octnum[50], i=0; printf("Enter any Decimal number: "); scanf("%d", &decnum); while(decnum != 0) { octnum[i] = decnum%8; i++; decnum = decnum/8; } printf("\nEquivalent Octal Value = "); for(i=(i-1); i>=0; i--) printf("%d", octnum[i]); getch(); return 0; }

As the above program was written under **Code::Blocks** IDE, therefore here is the sample run after successful build and run.
This is the first snapshot of the sample run:

Now supply any decimal number say 159 and press ENTER key to see its equivalent octal value as shown in the second snapshot of the sample run given here:

- Receive any decimal number from user at run-time as input
- Create a while loop that runs until the value of entered decimal number
is not equal to zero (0). In other words,
**while**loop runs until the value of**decnum**(given decimal number by user) becomes 0 - Let's suppose that user has entered
**159**as input. Therefore at first run of the**while**loop, the condition**decnum!=0**or**159!=0**evaluates to be true, therefore program flow goes inside the loop - And
**decnum%8**or**159%8**(while dividing the number 159 by 8, we will get a remainder as 7) or**7**gets initialized to**octnum[i]**or**octnum[0]**(as**i**holds 0 as its initial value) - Now the value of
**i**gets incremented and becomes 1. And then**decnum/8**or**159/8**or**19**gets initialized to**decnum** - Now the program flow goes back to the
**while**loop and checks the condition, that is**decnum!=0**or**19!=0**evaluates to be true, therefore program flow again goes inside the loop, and**decnum%8**or**19%8**or**3**gets initialized to**octnum[i]**or**octnum[1]**. - The value of
**i**gets incremented and becomes 2. And then**decnum/8**or**19/8**or**2**gets initialized to**decnum**. Program flow goes back again to the**while**loop and checks the condition - That is
**decnum!=0**or**2!=0**evaluates to be true, therefore program flow goes inside the loop again - And
**decnum%8**or**2%8**(talking about only whole number, 2 can not be divided by 8, therefore 2 will be the remainder in this case) or**2**gets initialized to**octnum[i]**or**octnum[2]** - The value of
**i**gets incremented and becomes 3. And**decnum/8**or**2/8**or**0**gets initialized to**decnum**. - Program flow again goes back to the
**while**loop, and checks the condition, that is**decnum!=0**or**0!=0**evaluates to be false, therefore program flow does not goes inside the loop (that is, now the program flow exits from the loop) - Now we have stored all the equivalent octal digit one by one of given decimal number in an array named
**octnum[]**here - We have to create a
**for**loop, that will be initialized with one less then the current value of**i**. As at last run of the**while**loop, we have incremented the value of**i**. But as**decnum**becomes 0, therefore program flow goes out from the loop, but**i**holds its value as one more than the required value, therefore we have to delete or skip last index or number. - We have to print the octal digit one by one in reverse order. That is, loop runs from
**i-1**to**greater than or equal to**to 0 - Inside the loop, print the octal digit one by one
- In this way, we have printed the equivalent octal value of given decimal number by user at run-time

Now let's create same purpose program that is to convert any given decimal number into its equivalent octal value but without using any modulous operator as shown here in the program given below.

// Decimal to Octal in C without Modulous Operator // ----codescracker.com---- #include<stdio.h> #include<conio.h> int main() { int decnum, octnum[100], i=0, temp, chck, rem; printf("Enter any Decimal number: "); scanf("%d", &decnum); while(decnum!=0) { temp = decnum/8; chck = temp*8; rem = decnum - chck; octnum[i] = rem; i++; decnum = temp; } printf("\nEquivalent Octal Value = "); for(i=i-1; i>=0; i--) printf("%d", octnum[i]); getch(); return 0; }

Here is the final snapshot of the sample run:

- As we all knows that modulous operator (%) only finds the remainder while dividing any number with any other number
- So here we have to find out the remainder while dividing the number
**decnum**by 8 - Therefore, we have simply divided the number by 8 and initialized its value to a variable say
**temp** - And then we have again multiply the same number
**temp**with 8 and intialized to another variable say**chck** - Now subtract and initialize the value of
**decnum - chck**(here**decnum**is the original number) to a variable say**rem** - For example, let's suppose user has supplied
**159**as input - Therefore at first run of the
**while**loop,**decnum/8**or**159/8**or**19**gets initialized to**temp** - And
**temp*8**or**19*8**or**152**gets initialized to**chck** - Then
**decnum-chck**or**159-152**or**7**gets initialized to**rem** - Now if you will replace the three line code with
**rem = decnum%8**, you will get the same value that is 7 will be initialized to**rem**here

Therefore, we have successfully replaced modulous operator and created the program for same purpose but without using any modulous operator as shown in the above program.

Now let's create a function-driven program for same purpose. The question is, **Write a program in C that converts
Decimal number to Octal number using user-defined function**. The answer to this question is:

// Decimal to Octal in C using Function // ----codescracker.com---- #include<stdio.h> #include<conio.h> void DecToOct(int dec); int octnum[50]; static int i; int main() { int decnum; printf("Enter any Decimal number: "); scanf("%d", &decnum); DecToOct(decnum); printf("\nEquivalent Octal Value = "); for(i=(i-1); i>=0; i--) printf("%d", octnum[i]); getch(); } void DecToOct(int dec) { while(dec != 0) { octnum[i] = dec%8; i++; dec = dec/8; } }

Here is the final snapshot of sample run of above program:

Here we have declared the variable **i** as static variable. Because static variable remembers its previous value.'
And we have not initialized the value of **i** with 0, because static variable holds 0 as its initial value.
That is if you will not initialize any value to any static variable, therefore 0 gets automatically initialized to it.
We have created the array **octnum[]** and the variable **i** outside the **main()** function so that both
the array and variable stays known throughout the program, that is inside both the function namely, **main()**
and **DecToOct()**.

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.