How To Write a C Program To Convert Decimal Number To Octal Number in C Programming Language ?
Steps to Convert Decimal to Octal :
- Accept the given decimal number
- If the number is less than 8 the octal number is the same
- If the num > 7 then Divide the number with 8
- Write down the remainder
- Do steps 3 and 4 with the quotient till that quotient is less than 8
- Write the remainders in reverse order (bottom to top)
- The resultant is the equivalent octal number to the given decimal number
Solution For C Program To Convert Decimal Number To Octal Number:
#include<stdio.h> #include<conio.h> #include<math.h> void dec_oct(long int num) // Function Definition { long int rem[50],i=0,length=0; while(num>0) { rem[i]=num%8; num=num/8; i++; length++; } printf("nOctal number : "); for(i=length-1;i>=0;i--) printf("%ld",rem[i]); } //================================================ void main() { long int num; clrscr(); printf("Enter the decimal number : "); scanf("%ld",&num); dec_oct(num); // Calling function getch(); }Output :Enter the decimal number : 20 Octal number : 24
Explanation of C Program :
We are dividing the number by 8 and storing the reminder in the rem[] array.
while(num>0) { rem[i]=num%8; num=num/8; i++; length++; }
Original number is then divided by 8 and
num = num / 8;
We are repeating the steps until ‘num’ is greater than 0.
while(num>0) { --- Logic of --- the Code }
Now we are printing the reminders stored in an array in reverse order so that equivalent octal number gets printed.
for(i = length-1 ; i >= 0 ; i--) printf("%ld",rem[i]);
Tags: C Program To Convert Decimal Number To Octal Number, write a c program to convert decimal to octal, c program to convert decimal to octal using while loop, write a c program to convert decimal number to octal number, c program to find decimal to octal, integer to octal in c, program to convert decimal to octal in c using recursion, c program to convert decimal to octal without using array, c program to convert decimal to octal and hexadecimal.
Learn More :
C Program
- Using Bash to input stuff into c program
- Difficult C Programming Questions
- Write a c program to find largest among three numbers using binary minus operator three numbers using binary minus operator
- PRINTING ASCII VALUE USING C PROGRAM
- MULTIPLICATION OF TWO MATRICES USING C PROGRAM
- FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING
- Write A C Program To Find Out Transport Of A Matrix
- Factorial of 100 in C Program
- Multiplication of large numbers in c
- Division of Large Numbers in C Program
- BINARY SEARCH USING C PROGRAM
- BINARY SEARCH THROUGH RECURSION USING C PROGRAM
- FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
- FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
- FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
- FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
- REVERSE A NUMBER USING RECURSION IN C PROGRAM
- SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
- Write A C Program For Swapping Of Two Arrays
- SWAPPING OF STRINGS USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
- CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM
- C or C++ Program To Find Bonus Amount