Program to Convert Decimal number into Octal : Number System : Download
Logic of This Program :
- Accept Number from User
- As we are dividing number by 8 , we have only remainders 0,1,2,3,4,5,6,7 etc
- Take an array of remainder i. rem[20];
- Let ,Initial elements in rem[] be Zero
- In each iteration Store Remainder in the Array & Divide original number by 2
- Also Count the number of remainders stored
- To Convert into Binary Just Reverse Remainder array
Program :
#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