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
Output is:

You should convert positive numbers only.

## All 10 Replies

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<<"\nConvert the decimal into the following:";
cout<<"\n0-[B]inary";
cout<<"\n1-[O]ctal";
cin>>convert;
return convert;
}

long int toBinary (long int a)
{
long int num;

cin>>num;

while (num>0)
{
a=a/2;
}
}

long int toOctal(long int a)
{
long int num;

cin>>num;

while(num>0)
{
a=a/8;
}

}
long int toHex (long int a)
{
long int num;
int ctr;

cin>>num;

num=a%16;
ctr++;

for (int ctr=a;ctr>0;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[10];
}

// add x to this multiset
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();

IMultiSet Q = new IMultiSet();

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).

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