Teacher gave a question today to find Base is 2 and power is 3. so that Answer should be 8. so to find that she told us to use Recursion and do it. i done it in the normal way. but when i do it in the Recursion way. one error coming saying too Few Parameters

please check

#include<stdio.h>

int multi(int,int);

main()
{
int x=0,y=0;


printf("Enter Your Base Number:");
scanf("%d",&x);


printf("Enter the Power Number:");
scanf("%d",&y);

printf("The power of %d is %d",x,multi(x,y));

}

int multi (int x, int y)
{
int ans=0;
 if(y<=1)

	 return 1;

 else
	{
	 ans=(x*multi(x,y-1));
	 return ans;
	 }
	 }

PLEASE HELP ME IN THIS

Recommended Answers

All 7 Replies

here's your problem:

if(y<=1)
return 1;

it should be:

if(y==1)	
		return x;
	if(y<=0)	
		return 1;

.

thanks my bad. in my coding i have put if(y<=1) it should be ( y<1) only . no equal sign.

thanks guys. thanks for the quick reply

yeah, i guess mine was redundant. i still dont think well in recursion.

The program you have written using recursion is logically wrong..

You have written the code if (y<=1) return=1
How can 2 power -1 will be equal to one..

So correct your program first and then see the results..

because he doesnt have to handle negative powers for his CS 101 class.

silly.

i agree. i thought about including that in my original answer, but decided to stick to the question as presented. some of the posters here (not necessarily this one) sort of implode if you throw too much at them.

anyhow... yeah, change your answer variable to type "double" , use the absolute value of the power to calculate the answer, and then throw this in at the end: if (pow<0) ans = 1.0/ans; pretty simple, really.

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.