DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   octal conversion (http://www.daniweb.com/forums/thread14876.html)

gowswan Dec 1st, 2004 9:29 am
octal conversion
 
sir, ihave a doubt on converting a inputed decimel number to a octal number
will u help me?

jwenting Dec 1st, 2004 9:54 am
Re: octal conversion
 
you mean you want to print an octal representation of a decimal number?
Check out your language reference for itoa.

varunrathi Dec 1st, 2004 10:08 am
Re: octal conversion
 
Here`s the code to convert a decimel number in any desired base

#include<iostream.h>
#include<conio.h>
#include<stdio.h>


void main()
{
clrscr();
int d,b=1,r,a,q,c = 0,s;
char n[100];

cout<<"enter the no.:";
cin>>d;
cout<<"\nenter base :";
cin>>b;

while(d>0)
{
q = d/b;
r = d%b;
d = q;

if(r>=10)
{
n[c] = 55 + r;
c++;
}
else
{
n[c] = 48 + r;
c++;
}
}
n[c] = '\0';

cout<<"\nthe no. in base "<<b<<" is : ";
for(int i = c-1;i>=0;i--)
{
cout<<n[i];
}

getch();


}

jwenting Dec 1st, 2004 11:12 am
Re: octal conversion
 
don't give people complete programs, it stops them from thinking for themselves...

Or if you do include some subtle logic errors that they'll have to solve to make it work.

And if you do give sample code make it proper code.
void main() is an abomination, <iostream.h> etc. are deprecated

Dave Sinkula Dec 1st, 2004 11:21 am
Re: octal conversion
 
>#include<iostream.h>
>#include<conio.h>

Nonstandard headers.

>#include<stdio.h>

Unused header.

>void main()

Incorrect.

>clrscr();

Unnecessary and a nonstandard function.

> n[c] = 55 + r;
> n[c] = 48 + r;

ASCII hacks.

>getch();

Nonstandard function.


All times are GMT -4. The time now is 4:34 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC