all i can think i will type here and please explain the others.thank you
this is just to enhance my knowledge about oop.The info i got now isn't enough...
q1
Write a simple program to ask user to input five digits number. Using the input, output the five digits number in reverse order.
solution

#include<stdio.h>

# include<iostream.h>

void main()
{
int a,n,s=0,t;
cout<<"Enter the number=";
cin>>a;
for(n=a;n!=0;n=n/10)>>this is the loop i think to get it in reverse orders correct??
{
t=n%10;<<to get the last digit
s=(s*10+t);<<<<to get output question and make it in  order but i am unsure
}
cout<<"The Reverse No. is "<< s<<endl;
}

i think using modulus 10 gets the last digit in the digit we inputed and doing modulus 10 again get the second last digit.
am i correct?

q2
Write a program that ask user to input an integer. Pass the input to functions toBase2(), toBase4(), toBase8(), for which each function will output the equivalent of the integer in Base 2, Base 4, and Base 8 respectively.
the equation i guess
89
in base 8 is:
1 * 8 ^ 0 + 3 * 8 ^ 1 + 1 * 8 ^ 2
in base 4:
1 * 4 ^ 0 + 2 * 4 ^ 1 + 1 * 4 ^ 2 + 1 * 4 ^ 3
in base 2:
1 * 2 ^ 0 + 0 * 2 ^ 1 + 0 * 2 ^ 2 + 1 * 2 ^ 3 + 1 * 2 ^ 4 + 0 * 2 ^ 5 + 1 * 2 ^ 6

#include<iostream>
#include<iomanip>
using namespace std;
void tobase2(int a)
{
	int z;
	cout<<a<<" in base 2: ";
	while(a>0)
	{
		z=a%2;>this is to get the remainder digit left of divide by 2  of 1 or 0
		a/=2;>>i wonder what's this use?to make it more efficient?
		cout<<z;
	}
	cout<<endl;
	return;
}
void tobase4(int a)
{
	int z;
	cout<<a<<" in base 4: ";
	while(a>0)
	{
		z=a%4;>>to get the  remainder of divide by 4 i guess?
		a/=4;>>please explain
		cout<<z;
	}
	cout<<endl;
	return;
}
void tobase8(int a)
{
	int z;
	cout<<a<<" in base 8: ";
	while(a>0)
	{
		z=a%8;>>to  get the  remainder of divide by 8 i guess?
		a/=8;>please explain
		cout<<z;
	}
	cout<<endl;
	return;
}


int main()
{
	int a;
	cout<<"Enter an integer: ";
	cin>>a;>>normal input by user
	tobase2(a);
	tobase4(a);
	tobase8(a);
	return 0;
}

q3
Modify Question 2 so that only one function is created called toBase() which will receive two arguments which is the integer to convert and the base to which the integer will be converted. This function can be used to convert any number to any base. Ask the user to input an integer. Then using the function, iteratively call the function toBase() which will output the integer from Base 2 until Base 10.

so this program receives only base and integer user and outputs of the desired integer in the base is it?
and then it displays the values of the received integer from 2 to 10.
this is wat i understand.
i only got the integer from 2 to 10.
any idea on doing for the desired

#include<iostream>
#include<iomanip>
using namespace std;
void toBase( int a, int b)
{
	int z;
	cout<<a<<" in base "<<b<<": ";
	while(a>0)
	{
		z=a%b; >>>>to get the remainder and make it divide again.
		a/=b;>>not sure need help here
		cout<<z;
	}
	cout<<endl;
	return;
}
int main()
{
	int a, b;
	cout<<"Enter an integer: ";
	cin>>a;
	for(b=2;b<11;b++)>>the loop from base 2 to 10
	{
		toBase(a, b);
	}
	return 0;
}

this only input from 2- 10 base of the required integer.

but i need more understanding of this coding.your help is so appreciated
and this is my first assignment for OOP in c++

I think that the problem is not the code, but your maths.

Let us start with a number, e.g 89 as use used. What does writing 89 actually mean: that there are 8*10 + 9.

If you write in base 4 , say 31, you are saying that you have 3 groups of 4 + 1 extra. ie. 3*4+1.

The code you are having trouble with is bascially to convert from a normal base 10 system to the required base.

So put simple, for base4 you need to use multiples of 4 e.g.
4, 16, 64 etc. and express the number in multiples of them plus a units number.

e.g. 47 : well you could write (47) units, but what about : (11) lots of four and 3 units. Then you might like (2) lost of sixteen and (3) lots of 4 and 3 units. i.e in base 4: 233.

To get that number in computer code you, first see what the remainder is when you do Number/Base e.g. 47/4 the remainder is 3. (In C++ that is expresses as 47 % 4). Then you see what you get when you divide by 4 but ignore the remainder e.g.
47/4 = 11. (In C++ with integers , that is 47/4). Now you also have a shorthand that is that you could write a=47; a=a/4; but you can also write a/=4; which is the same.

Hope that helps.

C++ is the easy bit, but it really exposes any point that you don't completely understand the maths you are programming. I will always employ a maths graduate who can't code over a c++ how doesn't know maths.

for your q1 this is the code that will reverse the digit.. 3 integers, you should modify this to 5 digit..

temp=x;
x=z;
z=y;
y=temp;

for your q1 this is the code that will reverse the digit.. 3 integers, you should modify this to 5 digit..

temp=x;
x=z;
z=y;
y=temp;

This is a bad solution, you should not do it this way as this relies on you know A exactly how many digits will be entered at compile time. Plus it wouldn't be quite that simple since 363 would correspond to x, and x y and z.

Please rad Stu's post, it explains how to do it perfectly.

Chris

This is a bad solution, you should not do it this way as this relies on you know A exactly how many digits will be entered at compile time. Plus it wouldn't be quite that simple since 363 would correspond to x, and x y and z.

Please rad Stu's post, it explains how to do it perfectly.

Chris

I agree with chris. Considering your first question, another way to get the digits in reverse order is storing remainder, keep dividing the original no by 10 until the number comes to a single digit.

1) store number % 10

2) change value of number to number/10

3) repeat above two steps while number is greater than 10

You will get the digits of the given number in a reverse order.

This article has been dead for over six months. Start a new discussion instead.