[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Is it possible to calculate the power of a number in C without a loop?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 22
Thread images: 3

File: TO_THIS.png (739KB, 954x1400px) Image search: [Google]
TO_THIS.png
739KB, 954x1400px
Is it possible to calculate the power of a number in C without a loop?
>>
>>57198010
Yes.
>>
>>57198050
substantiate your claim
>>
>>57198010
No.
>>
>>57198104
Recursion
>>
>>57198159
do you have anything to back that up?
>>
>>57198189
Of course.
>>
>>57198189
EVERY loop can be transformed in a recursive function
And vice versa
>>
>>57198206
please proceed
>>
Do your own assignment OP. Anything you can do with a loop you can do with recursion. (Ignoring maximum stack size and compiler restraints)
>>
>>57198443
This isn't an assignment. I'm learning C by myself.
>>
int pwr(int num, int exp){
if(exp ==1)
return num;
return num * pwr(num, --exp);
}
Good luck with your assignment bro.
>>
>>57198010
>power of a number
int pow_of_1(int n)
{
return n;
}
>>
  int power (int n, int p) {
if (n == 1) {
return 1;
} else if (n == 2) {
if (p == 1)
return 2;
else if (p == 2)
return 4;
else if (p == 3)
return 8;
else if (p == 4)
return 16;
else if (p == 5)
return 32;
else if (p == 6)
return 64;
else if (p == 7)
return 128;
else if (p == 8)
return 256;
else if (p == 9)
return 512;
else if (p == 10)
return 1024;
} else if (n == 3) {
if (p == 1)
return 3;
else if (p == 2)
return 9;
else if (p == 3)
return 27;
else if (p == 4)
return 81;
else if (p == 5)
return 243;
else if (p == 6)
return 729;
else if (p == 7)
return 2187;
else if (p == 8)
return 6561;
else if (p == 9)
return 19683;
else if (p == 10)
return 59049;
}
// und so weiter...
// you get the idea!
}


There you go, you can thank me later.
>>
File: Untitled.png (60KB, 423x436px) Image search: [Google]
Untitled.png
60KB, 423x436px
>>57198958
>>
File: 1472345377152.png (2MB, 1000x967px) Image search: [Google]
1472345377152.png
2MB, 1000x967px
>>57198958
Wow, that's depressing what you just wrote there.
>>
>>57198958
2017-0.2
>>
use inline assembly with FYL2X and F2XM1
>>
fun power(n, p, c)
if p==1
return n*c
else
return power(n, - - p, c)*p
>>
>>57198010

Use recursion you dumb fuck
>>
#include <stdio.h>

int power(int n1, int n2);

int main()
{
int base, powerRaised, result;

printf("Enter base number: ");
scanf("%d",&base);

printf("Enter power number(positive integer): ");
scanf("%d",&powerRaised);

result = power(base, powerRaised);

printf("%d^%d = %d", base, powerRaised, result);
return 0;
}

int power(int base, int powerRaised)
{
if (powerRaised != 1)
return (base*power(base, powerRaised-1));
else
return 1;
}
>>
>>57198010
pow(2, 32)
Thread posts: 22
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.