google.com, pub-6167773875660516, DIRECT, f08c47fec0942fa0 Write a c program to calculate x^n using recursion - 2nd puc computer science

Write a c program to calculate x^n using recursion

Write a c program to calculate x^n using recursion

#include<stdio.h>

#include<conio.h>

float expo(float a, int n)

{

if(a==0)

                  return 0.0;

         else

                  if(n==0)

                           return 1.0;

                  else

                           if(n>0)

                                    return(a*expo(a,n-1));

                           else

                                    return(1/a*expo(a,n+1));

}

void main()

{

float expo(float, int);

float x;

int n;

clrscr();

printf("Enter the base: ");

scanf("%f",&n);

printf("Enter the exponent: ");

scanf("%d",&n);

printf("%f^%d=%f",x,y,expo(x,n));

getch();

}

Output:

 

run 1:

Enter the base: 2

Enter the exponent: 4

2.000000^4=16.000000

 

run 2:

Enter the base: 0

Enter the exponent: 5

0.000000^5=0.000000

 

run 3:

Enter the base: 3

Enter the exponent: 0

3.000000^0=1.000000
Write a c program to calculate x^n using recursion Write a c program to calculate x^n using recursion Reviewed by Vision Academy on December 14, 2021 Rating: 5

No comments:

CheckOut

Powered by Blogger.