Create a program that would convert a decimal number to either binary, octal or hexadecimal counterpart. Your program should ask the user for decimal with a data type of a long integer (4 bytes or 32bits in length or having a range of +2147483647 to -2147483648, or 4294967296 if unsigned). Then, it would give the user choices on what number format s/he would convert the decimal number. Example output:

Enter a positive integer:
inary
[O]ctal
[H]exadecimal
Output is:

You should convert positive numbers only.

## Recommended Answers

Your program <snip> You should

That means you, not us. But seriously, show us what you have tried, don't just drop off your assignment.

Okay, good start. Now what is the issue with it? Which portion doesn't work?

You have mixed up the way your variables are used in your functions. For instance you call `toOctal` on line 19 without any arguments, but `toOctal` take a `long int` argument and the way they are used in the function will give you an infinite loop! The function `toHex` also …

## All 10 Replies

Your program <snip> You should

That means you, not us. But seriously, show us what you have tried, don't just drop off your assignment.

``````#include <iostream.h>

int choice();
long int toBinary(long int a);
long int toOctal(long int a);
long int toHex(long int a);

long int main ()
{
int convert=choice();

switch (convert)
{
case (0):
toBinary ();
break;

case (1):
toOctal ();
break;

case (2):
toHex ();
break;
}
return 0;
}

long int choice ()
{
int convert;
cout<<"Enter Your Choice of Conversion";
cout<<"\nConvert the decimal into the following:";
cout<<"\n0-[B]inary";
cout<<"\n1-[O]ctal";
cout<<"\n2-[H]exadecial";
cin>>convert;
return convert;
}

long int toBinary (long int a)
{
long int num;
long int answer=0;

cout<<"Please enter a decimal";
cin>>num;

while (num>0)
{
answer=a%2;
a=a/2;
cout <<answer<<" ";
}
return answer;
}

long int toOctal(long int a)
{
long int num;
long int answer=0;

cout<<"Please enter a decimal: ";
cin>>num;

while(num>0)
{
answer=a%8;
a=a/8;
cout<<answer<< " ";
}

}
long int toHex (long int a)
{
long int num;
int answer[]=0;
int ctr;

cout<<"Please enter a decimal";
cin>>num;

num=a%16;
ctr++;
answer[ctr]=num;

for (int ctr=a;ctr>0;ctr--)
{
cout<<answer[ctr];
}
return num;
}

}``````

Okay, good start. Now what is the issue with it? Which portion doesn't work?

Okay, good start. Now what is the issue with it? Which portion doesn't work?

[Actually, it works but our instructor told me that my program is erroneous... I don't know which part of my code that makes it erroneous... Anyone help me to please clearly modify it..

Thank You so much...]

You have mixed up the way your variables are used in your functions. For instance you call `toOctal` on line 19 without any arguments, but `toOctal` take a `long int` argument and the way they are used in the function will give you an infinite loop! The function `toHex` also has a number of strange things in it, like `answer[] = 0;` .

Well, anyway I just had already fixed the said arguments that affects the part of the code

``````public class IMultiSet{
private int multiset[];

public IMultiSet(){
multiset = new int;
}

// add x to this multiset
void add(int x){
if(x < multiset.length && x >= 0)
multiset[x]++;
}

// remove x from this multiset if there are occurrences of x is
void remove(int x){
if(x < multiset.length && x >= 0)
if(multiset[x]>0) multiset[x]--;

}

public IMultiSet sum(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 || S.multiset[i]!=0){
if(this.multiset[i] >= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

public IMultiSet intersection(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] <= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

public IMultiSet difference(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]==0)
tmp.multiset[i] = this.multiset[i];
else if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] - S.multiset[i] <= 0)
tmp.multiset[i] = 0;
else
tmp.multiset[i] = this.multiset[i] - S.multiset[i];
}

}
return tmp;

}

// return the string equivalent of this multiset in format {x:a, y:b, z:c }
public String toString(){
String tmp = "{ ";
for(int i=0;i<multiset.length;i++){
if(this.multiset[i] != 0){
tmp+= i + ":" + this.multiset[i] + ",";
}
}
tmp+= "\b }";
return tmp;
}

public static void main(String []args){
IMultiSet P = new IMultiSet();
P.add(2);P.add(2);P.add(2);P.add(2);P.add(2);
P.add(5);P.add(5);P.add(5);
P.add(6);P.add(6);
P.add(8);P.add(8);

IMultiSet Q = new IMultiSet();
Q.add(1);Q.add(1);Q.add(1);
Q.add(2);
Q.add(6);Q.add(6);Q.add(6);
Q.add(7);Q.add(7);
Q.add(9);Q.add(9);Q.add(9);Q.add(9);Q.add(9);

System.out.println("The value of P");
System.out.println("P = " + P.toString());
System.out.println("The value of Q");
System.out.println("Q = " + Q.toString());
System.out.println("\nP U Q = " + P.sum(Q));
System.out.println("P Intersection Q = " + (P.intersection(Q)).toString());
System.out.println("P - Q = " + (P.difference(Q)).toString());

}

}``````

What is the question? And is this supposed to be C++ code, or Java?

"P U Q" is normally called a "union", not a "sum".

I'm not sure why you keep checking that elements of multiset are not zero in sum(), intersection() and difference(). Zero is a perfectly reasonable value, and it needs to be handled (not skipped).

P.S. When posting completely unrelated code with a new question, please start a new thread. And did you get your questions answered from your previous assignment?

This program shouldn't be as near as long as that, by using the C Plus Plus website you can do what your doing, in about 10 lines, with the exception of binary... and hex shouldn't be too hard to convert to binary, considering that every 4 bits is the same as 1 hexadecimal place-value.

How to program this?

Test case 1
Enter a number: 100
Convert to - Type A binary B octal C hex): A
In binary: 0b1100100

Test case 2
Enter a number: 31
Convert to - Type A binary B octal C hex): C
In hexadecimal: 1F
Thats the using python.
thats the

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.