# task of nth power without math.h

Page 1 of 1

## 7 Replies - 1222 Views - Last Post: 06 January 2013 - 06:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=305779&amp;s=789ae4986262ab68a2858d31e1de4026&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ahmedali1991993

Reputation: 0
• Posts: 4
• Joined: 06-January 13

# task of nth power without math.h

Posted 06 January 2013 - 03:58 AM

```#include<iostream>
using namespace std;

void main()
{
double a,d;
int b;
cout<<"*********nth square********"<<endl<<endl;
cout<<"enter number for which power is to be taken"<<endl;
cin>>a;
cout<<"enter the power you want to take for the entered number"<<endl;
cin>>b;

if(b==2)
{
d=a*a;
cout<<"square of entered number = "<<d<<endl;
}
else if(b==3)
{
d=a*a*a;
cout<<"cube is "<<d<<endl;
}
}

```

i want that that it should work as "a" should be multiplied by it self as many times as
"b" is entered by the user. how can i do it, i cant understand what codition or loop to use here??

This post has been edited by stayscrisp: 06 January 2013 - 05:41 AM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

## Replies To: task of nth power without math.h

### #2 Ryano121

• D.I.C Lover

Reputation: 1458
• Posts: 3,285
• Joined: 30-January 11

## Re: task of nth power without math.h

Posted 06 January 2013 - 05:05 AM

I suggest you read over the for loop again. You want to loop from 0 to n and multiply your result by the number each time around.

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3466
• Posts: 12,351
• Joined: 08-August 08

## Re: task of nth power without math.h

Posted 06 January 2013 - 06:25 AM

And for the love of God, make it a function!!!

### #4 Ryano121

• D.I.C Lover

Reputation: 1458
• Posts: 3,285
• Joined: 30-January 11

## Re: task of nth power without math.h

Posted 06 January 2013 - 06:27 AM

Also I forgot to say

int main() == GOOD

### #5 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: task of nth power without math.h

Posted 06 January 2013 - 04:15 PM

You need to loop "b" times while multiplying "a" by itself each iteration.

A for loop seems most suitable.

```for(int i=0; i<b; i++) { //loop "code" b times
//code
}

```

### #6 Skydiver

• Code herder

Reputation: 4839
• Posts: 15,978
• Joined: 05-May 12

## Re: task of nth power without math.h

Posted 06 January 2013 - 04:47 PM

<sarcasm>
I don't see the problem. Just don't include math.h, but write down:
```extern double pow(double x, double y);

```

</sarcasm>

Seriously, though, everyone seem to be assuming that this exercise is to teach for and/or while loops. What about the case when y is not an integer like in 52.5? Is it possible to loop two and a half times?

Nevermind, on closer look, the number b in the OP is always on integer for ab.

This post has been edited by Skydiver: 06 January 2013 - 04:46 PM

### #7 Ryano121

• D.I.C Lover

Reputation: 1458
• Posts: 3,285
• Joined: 30-January 11

## Re: task of nth power without math.h

Posted 06 January 2013 - 04:49 PM

Shouldn't be a problem in this case as b is an int.

### #8 Skydiver

• Code herder

Reputation: 4839
• Posts: 15,978
• Joined: 05-May 12

## Re: task of nth power without math.h

Posted 06 January 2013 - 06:35 PM

Yep, with b as an integer, a for loop seems ideal.

Except how does one deal with negative exponents? (ex. 3.2-3) Compute using the absolute value and then get the reciprocal?