Answer to Question #194610 in C for Muhammad Haris

Question #194610

Write a function power(a, b), to calculate the value of a raised to b, using: (a) non-recursive method (b) recursion 


1
Expert's answer
2021-05-19T05:04:54-0400


#include<stdio.h>


int powerNonRecursive(int a, int b);
int powerRecursion(int a, int b);


int main()
{
    int a, b;
    printf("Enter base number: ");
	scanf("%d",&a);


    printf("Enter power number(positive integer): ");
    scanf("%d",&b);
	printf("Non-recursive: %d^%d = %d\n\n",a,b, powerNonRecursive(a, b));
	printf("Power pecursion: %d^%d = %d\n\n",a,b, powerRecursion(a, b));

	getchar();
	getchar();


    return 0;
}


int powerNonRecursive(int a, int b)
{
	int i=0;
	int result=1;
    for (i = 1; i <=b; i++) 
    {
       result=result*a;
    }
	return result;
}


int powerRecursion(int a, int b)
{
    if (b != 0)
        return (a*powerRecursion(a, b-1));
    else
        return 1;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS