Sunday, March 16, 2014

Decimal to Octal converter (basic way)


Here is a basic way to get the octal value of a decimal number. In this program, a for loop is used which will get the remainder, decrease the number by dividing it by 8 and populate the array. At the end, we will simply print the array in reverse order.                                                                                                                                                                                                                                                                      
 
#include<iostream.h>

#include<conio.h>

void main()

{

int num,rem,div,i=0,a[100];

cout<<"Enter the number :  ";

cin>>num;                       //getting the number

for (div=num/8;div>0;)

{

if (i==0)

rem=num%8;

else

{

rem=div%8;                      //dividing the number by 8 to get remainder and getting new number

div=div/8;

}

a[i]=rem;                      //populating the array with remainder

i++;

}

cout<<endl<<"The Octal Value of given number is :  ";

for (int k=i-1;k>=0;k--)

cout<<a[k];                       //printing the array in reverse order

getch();

}

Widgets

 

Copyright @ 2014 CPP Fuzz.