PDA

View Full Version : simple loops


Naikon
12-05-2007, 01:50 AM
Hi, I have a small simple problem and I would greatly appreciate assistance.

I have printed a simple table using a loop as follows:

Base Exponent Result //result=base raised to exponent//
2 0 1
2 1 1
2 2 1
2 3 1
2 4 16


Basically, I want a user to enter a base and min and max exponents, and without using pow() function, calculate each base raised to the exponent given a max and min exponent.
Sorry, my code is really, really bad I am a complete noob.

I just want to learn programming but this is an issue for me:(
How do I get the output such that the base is raised to each exponent in the range for each iteration?

Thanks.


for(i=min; i <= max; i++)

{

answer = 1;

for(min=min; min <= max; min++)

{
answer = max*min;
}

Spookster
12-05-2007, 02:22 AM
Well think about what a number to a power is. For example:

2^2 = 2 * 2
2^3 = 2 * 2 * 2
2^4 = 2 * 2 * 2 * 2

You want to take the base the user enters which here is 2 and compute that base to the power indicated. In this case the power is a range of numbers from 2-4. Since you are not allowed to use the pow() function then you just have to use a loop to multiply it out.

base = entered_base
min = entered_min
max = entered_max

Once you have those three think about what you need to do with those three variables. You need to multiply the base by itself. The number of times you need to do this is going to be from min to max. So min and max become the basis for how many times your loop should execute. Your loop should execute starting at min and stopping at max. Each time the loop iterates you need to multiply your base with itself and add up the values til the loop stops.