DECIMAL TO OCTAL CONVERSION IN C
In this example, you will learn a program to convert decimal number to its equivalent octal number.
Decimal numbers have base 10 , and they use numbers between o to 9.
while, Octal numbers have base 8 , and they use numbers between 0 to 7.
( To understand this program , you should have prior knowledge about decimal to octal conversion. )
Program:
//Program to convert decimal number to octal number
#include<stdio.h>
#include<conio.h>
int DectoOct( int num);
int main( )
{
int num;
printf("Enter a decimal number:");
scanf("%d",&num);
printf("%d decimal number= %d in octal number", num, DectoOct(num) );
return 0;
}
int DectoOct(int num)
{
int OctalNumber=0, i=1;
while(num !=0)
{
OctalNumber += (num %8)*i;
num /= 8;
i*=10;
}
return OctalNumber;
}
#include<stdio.h>
#include<conio.h>
int DectoOct( int num);
int main( )
{
int num;
printf("Enter a decimal number:");
scanf("%d",&num);
printf("%d decimal number= %d in octal number", num, DectoOct(num) );
return 0;
}
int DectoOct(int num)
{
int OctalNumber=0, i=1;
while(num !=0)
{
OctalNumber += (num %8)*i;
num /= 8;
i*=10;
}
return OctalNumber;
}
Output:
Enter a decimal number: 10
10 decimal number= 12 in octal number
10 decimal number= 12 in octal number
Try this code by your own!
If you have any doubts , then feel free to drop a comment. I'll be happy to answer questions in the comment section.
Keep coding.....
Comments
Post a Comment