Here's how I made it work:
UPDATE: Safer code that accounts 0 below.
Doesn't work with non-even numbers (ie 0.5, etc).
Printable View
Here's how I made it work:
UPDATE: Safer code that accounts 0 below.
Doesn't work with non-even numbers (ie 0.5, etc).
Thanks a lot for writing down the full code. :) However I had my friend try it and it gives errors. Could it be that it contains C++?
well this is the other way of doing it, may be if its interesting to you
ssharishCode:if( power < 0 )
printf("Result %f", ( 1.0 / pow(base, ( power * -1 ))));
else
printf("Resilt %f", pow(base, power));
another thing you may want to consider is the case that num=0 (because you cannot do 0^(-3))
0 * X = 0.
0 / X = 0.
So there's no worry I think.
Thanks to all of you for your efforts! :) Eventually we got it to work with this code, which doesn't contain any C++. (The words are in Turkish now though, don't ask. :p I believe us is power). Thanks again! :)
Code:#include <stdio.h>
int main()
{
int us=0;
int taban=0;
float sonuc=1;
int sayac=1;
printf("Ussu alinacak sayiyi girin....:D");
scanf("%d",&taban);
printf("\nUssu giriniz......:D");
scanf("%d",&us);
if (us>0)
{
for(sayac=1;sayac<=us;sayac++)
{
sonuc *= taban;
}
}
else
{
for(sayac=1;sayac<=-us;sayac++)
{
sonuc *= (1 / (float)taban);
}
}
printf("%d uzeri %d = %.2f dir",taban,us,sonuc);
return 0;
}
Look at my code above. Don't forget to check for power == 0 (anything raised to 0 is 1) and number == 0 (division by 0 is illegal). Also, you have a bug, I realize.
Here's a condition:
number = 10
power = 3
Result should be 1000, right?
Let's check them loops:
First loop: number = number (10) * number (10) = 100
Second loop: number = number (100) * number(100) = 10.000
Just a bit change, considering that 0^(a) has sense for a > 0 (and it's not 0)
Code:int main()
{
double num;
double onum;
double pow;
cout << "Enter number: ";
cin >> num;
cout << "Enter power: ";
cin >> pow;
if (num == 0)
{
if (pow>0)
{
cout << "Ehi! I cannot do that! " << endl;
return 1;
}
// this is useless: num = 0;
}
else
{
onum = num;
if (pow > 0)
{
for (; pow > 1; pow--)
{
num *= onum;
}
}
else if (pow == 0)
{
num = 1;
}
else if (pow < 0)
{
for (; pow < 1; pow++)
{
num /= onum;
}
}
}
cout << "Result is " << num << endl;
return 0;
}
0^X = 0. What are you implying?
Depends on your calculator:
Windows Calc says:
0^0 = 1 (Which is false)
0^(-3) = Invalid
Powercalc says:
0^0 = Invalid
0^(-3) = 0
My ti calc says:
0^0 = Invalid
0^(-3) = Invalid
Still, the code doesn't check for division by 0.
This takes into consideration powers of 0 and negative when number is 0.Code:int main()
{
double num;
double onum;
double pow;
cout << "Enter number: ";
cin >> num;
cout << "Enter power: ";
cin >> pow;
if (num == 0)
{
if (pow <= 0)
{
cout << "Invalid number / power combination.\n" << num << " ^ " << pow << ": Invalid operation!\n";
return 1;
}
num = 0;
}
else
{
onum = num;
if (pow > 0)
{
for (; pow > 1; pow--)
{
num *= onum;
}
}
else if (pow == 0)
{
num = 1;
}
else if (pow < 0)
{
for (; pow < 1; pow++)
{
num /= onum;
}
}
}
cout << "Result is " << num << endl;
return 0;
}