473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I raise any number x to a positive power n using a for loop?

int base;
int exp;
int answer = 1
for(int i = 0; i<n; i++)
{answer*=base}


This code above works, I just don't understand the functionality. "answer*=base" doesn't seem to have anything to do with "i" or "exp". I don't understand the logic behind it all.
Feb 5 '12 #1
3 9553
Rabbit
12,516 Expert Mod 8TB
It won't work. At least not if that's all the code there is. It won't work unless somewhere else n is set to exp.
Feb 6 '12 #2
Excuse me, I wrote the code wrong. n is supposed to be the exp.
So it goes:

int base;
int exp;
int answer = 1;
for(int i=0; i<exp; i++)
{answer*=base;}

This format works, and when output the exponent of any base is printed.
I just don't understand {ans*+base;}
... how does this simple statement work? It leaves out both i and exp.??
Feb 6 '12 #3
Rabbit
12,516 Expert Mod 8TB
answer *= base is the same thing as answer = answer * base. i and exp doesn't have to be used in the actual calculation. It's just there to make sure base is multiplied the correct number of times. 2 to the power of 3 is 8. 2 is the base, 3 is the exp. But to calculate it, it's 2 * 2 * 2. There's no need to actually use exp in the calculation.
Feb 6 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: friend_sri | last post by:
Hi all I want to know how to read the serial number of usb using C program in linux OS. I want to use this in JNI as a native code. Please help. Thnx in advance
1
by: Rejimonb | last post by:
Can I fill a combobox without using a loop. Using a loop will be time consuming. Reji
3
by: greek | last post by:
the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is x= 1, n=0 1/x^n, n<0 x*x^(n-1), n>0 ...
3
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
5
by: boss1 | last post by:
hi all, i have a problem with loop in select statement.i m using code : <select name = "s" size = "1" > <option selected>P-Code</option> <option...
5
by: Henry J. | last post by:
I'm new to DB2. I want to remove large number of rows in a table without filling up the transaction log space. To this end we can repeatedly remove a fixed number of rows followed by commit. In...
5
by: dave816 | last post by:
Sorry for the Excel question in an Access forum...................I don't see an Excel forum and there's probably a reason for that but figured I'd give this a shot anyway. Again sorry, delete if...
1
by: Donnie Seibel | last post by:
I wrote a script to list all lines in a file with Perl. I am having trouble with the looping part of it. My script is supposed to look at the file and as long as the file is larger than the current...
2
by: Jack Wales | last post by:
I need to set up a text box in Visual Basic which will only allow positive numbers and will produce an error message if 1 - A negative number is entered, 2 - something not numeric is entered and 3 -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.