By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Need help with a program I am writing

P: 11
I need to write a program that will compute a number (base) raised to a power which can be an integer, negative or zero with out including the math library. This is what I wrote thus far. Please help me.

#include <iostream>
using namespace std;

int main()
{
double x, y;

cout << "Enter a base number: ";
cin >> x;
cout << "Enter a integer exponent: "
cin >> y;

double i = 1;
double product = x;

while ( y > i)
{
product *= x;
i++;
}

return 0;
}

My problem is how do I set up the while statement to compute lets say 4 raised to the -2? Do I include an if statement in the body? Your help is greatly appreciated.
Oct 28 '06 #1
Share this Question
Share on Google+
11 Replies


100+
P: 106
look hunny this program is so easy if you used functions to do the exponential part and then call that function from the main

here's the code i'v posted it today 4 someone asking your same question :

#include <stdio.h>
#include<conio.h>
#include<iostream.h>

int integerPower(int ,int ); //this is a prototype
void main() //void main or int main won't matter but i prefer void main
{ clrscr(); //just to clear the screen 4 you each time you run your program
int exp;
int base;

printf("Enter integer base and exponent:");
cin>>base>>exp;
cout<<base<<"to the power of"<<exp<<"equals="<<integerPower(base,exp);
getch(); //waits for a character to return to main program
}

int integerPower(int b,int e)
{
int product=1;

for(int i=1; i<=e; i++)
product*=b;
return product;
}
if you need any help with this code just post me ......
Oct 28 '06 #2

P: 11
This is my first lesson in c++ and I don't have a professor to teach me throughly so I am basically winging it. I must write the program only with the header file <iostream> nothing else. Also I should only use the while loop and take into consideration positive, negative values for the exponent.


I must say thanking you for responding and for your help. Do you have any other suggestion as to how to do this exercise with only the required header file?







look hunny this program is so easy if you used functions to do the exponential part and then call that function from the main

here's the code i'v posted it today 4 someone asking your same question :

#include <stdio.h>
#include<conio.h>
#include<iostream.h>

int integerPower(int ,int ); //this is a prototype
void main() //void main or int main won't matter but i prefer void main
{ clrscr(); //just to clear the screen 4 you each time you run your program
int exp;
int base;

printf("Enter integer base and exponent:");
cin>>base>>exp;
cout<<base<<"to the power of"<<exp<<"equals="<<integerPower(base,exp);
getch(); //waits for a character to return to main program
}

int integerPower(int b,int e)
{
int product=1;

for(int i=1; i<=e; i++)
product*=b;
return product;
}
if you need any help with this code just post me ......
Oct 29 '06 #3

100+
P: 106
hunny you don't need the other header files ...

#include<conio.h> i used it in order to write clrscr(); which as i told you clears the screen for you each time you see the output but you may not use it at all but each time you run your program you will see the same sentences that were written before so if you run this program many times you will have a very DIRTY screen ( the black screen for output)....

also #include<stdio.h> is not necessary i just used it coz i thought i may use printf and scanf but don't worry you can remove it at alllll

and about the for loop you can use any other loop just like the while but right now i can't provide you with the while loop coz i have to sleep now ...but promise to post you tommorow

by the way where r u from ??
Oct 29 '06 #4

100+
P: 106
Good MOrning !!

that's the while loop you want

while(y>=i)

{
product*=x;
i++ ;

}
your code was right except that part in while( y>i ) it should be (y>=i)

and also you shouldn't say double product=x; so plz remove that part
Oct 29 '06 #5

P: 11
Thank you so much for your help and time. I got the program to run but when I enter a negative number for the exponent it didn't give me the correct value. My task is to ensure that whatever value is entered for the exponent can be zero, positive or negative.




Good MOrning !!

that's the while loop you want

while(y>=i)

{
product*=x;
i++ ;

}
your code was right except that part in while( y>i ) it should be (y>=i)

and also you shouldn't say double product=x; so plz remove that part
Oct 29 '06 #6

100+
P: 106
Do you know why it gives you uncorrect answer when you enter a negative number ?????

i just want to make sure you understand what you r doing ...
Oct 29 '06 #7

P: 11
To be quite honest with you I can't see what I am doing wrong. Based on my understanding of the program if I enter base as 1.75 and the exponent as -2 the program should be able to compute the result by putting the answer as 1/base. I thought in the body of the while loop I would have to use an if statement but that didn't work either. Currently I don't have a professor who devotes time to teach the course work since our class is only for 50 minutes and most of the assignments are given for us to learn little details on our own.



Do you know why it gives you uncorrect answer when you enter a negative number ?????

i just want to make sure you understand what you r doing ...
Oct 29 '06 #8

100+
P: 106
that's pretty good .... im telling you that you r doing just fine ....
your program is so complicated now since you r not using functions but later on when you learn functions and how to call them from the main i want you to do the same program and see how hard it was before

anyway , your program is based on positive numbers only since your while loop only checks positive numbers
so when you enter a negative exponent this while loop will not be executed

while(y>=i ) if y= -3 for example this means that y< i so the condition of the while loop will not hold ......
its good of you to think of putting a IF statment inside the loop body but this IF won't be executed also coz he won't enter the loop .......

i hope you got that part ??!

so what if you said after : cout <<"enter an exponent"; cin>>y;
if (y<0)
{ y= -y ; }
now when he enters -3 or -4 the IF statement will deal with it as if it was 4 0r 3
that's what y= -y ; do .... i hope you got that ??
after that you can do your loop as before since now any negative exponent will be like positive one but you will face another small problem ?
can you tell what's it ??
the problem is now if the user entered 2 to the power of -3 the answer will be 8 the same as if he said 2 to the power of 3 ...but we want the result in case of -3 to be (1/8) not 8 ...so just say product=(1/product) ...
Oct 29 '06 #9

100+
P: 106
i can send you the code after these changes but it is a bit longer that before since now you have two while loops

you first check if (y<0) then do the first while and make product=(1/product)

ELSE
the second while which is he one you done before !!

anyway here's the code and if you didn't got it just mark on what you don't understand and post me

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ clrscr();
double x, y;
double product=1 ;

cout << "Enter a base number: ";
cin >> x;
cout << "Enter an integer exponent: ";
cin >> y;

int i = 1;

if (y<0)
{ y=-y;

while (y>=i)
{
product *= x;
i++;
}// end while

cout<<x<<"to the power of "<<y<<"equals="<<(1/product);
}//end if
else {

while ( y >= i)
{
product *= x;
i++;
}
cout<<x<<"to the power of "<<y<<"equals="<<product;
}//end else
getch();

}
and again don't care about CONIO.H " lol "
Oct 29 '06 #10

P: 11
LOL.

Well I tried running your program but it is not taking the value of the exponent to be negative since the if statement you wrote said:

if (y<0)
y = -y;

It is turning the exponent into a positive integer. I don't know if you are testing it first before you send me your explanation but I must say it is bring me closer to my result.

Whatever number is inputed as base raised to a negative number should give the right result. I am also testing the input on a calculator so we have some garbage value somewhere. Thanks again.










i can send you the code after these changes but it is a bit longer that before since now you have two while loops

you first check if (y<0) then do the first while and make product=(1/product)

ELSE
the second while which is he one you done before !!

anyway here's the code and if you didn't got it just mark on what you don't understand and post me

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ clrscr();
double x, y;
double product=1 ;

cout << "Enter a base number: ";
cin >> x;
cout << "Enter an integer exponent: ";
cin >> y;

int i = 1;

if (y<0)
{ y=-y;

while (y>=i)
{
product *= x;
i++;
}// end while

cout<<x<<"to the power of "<<y<<"equals="<<(1/product);
}//end if
else {

while ( y >= i)
{
product *= x;
i++;
}
cout<<x<<"to the power of "<<y<<"equals="<<product;
}//end else
getch();

}
and again don't care about CONIO.H " lol "
Oct 29 '06 #11

100+
P: 106
YEs i tested it well and it's very very right and working ...plz try running it again

i know it's not taking the exponent to be negative but that doesn't matter as long as you display ( 1/product) and not the product itself


but in the second while you just say cout<<product ....

i hope u got me !!! try it and post again
Oct 29 '06 #12

Post your reply

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