7 Replies - 553 Views - Last Post: 06 January 2013 - 06:35 PM Rate Topic: -----

#1 ahmedali1991993  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,085
  • 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!!!
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: task of nth power without math.h

Posted 06 January 2013 - 06:27 AM

Also I forgot to say

void main() == BAD

int main() == GOOD
Was This Post Helpful? 1
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • 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 
}


Was This Post Helpful? 1
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3535
  • View blog
  • Posts: 10,943
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • 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.
Was This Post Helpful? 2
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3535
  • View blog
  • Posts: 10,943
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1