Anyone know how to do this question?
Use recursion to implement the following recurrence relation f(x):
f(x)=1 where =1
f(x) = f((x+1)/2) +1 where x is odd
f(x) = f(x/2) +1 where x is even

This is what i did:

#include<stdio.h>

int func (int x)

int main()
{
	int x;
	printf("Please enter the value of x: ");
	scanf("%d", &x);

	int func=func(x);
	printf("%d",func) ;

	return 0;
}

int func (int x)
{
	if(x==1) 
		return 1;
	else if (x%2 ==1)
		return func ( (x+1)/2 ) + 1;
	else
		return func (x/2) + 1;
}

But it doesnt work.
Could you please kindly explain how a recursion work as well?
Thank youuuuu!

int func1(int x){
    //do something with int
   return func1(x-1);
}

see also this and this one

Inside a function when you call the same function then it is known as recursion function

Anyone know how to do this question?
Use recursion to implement the following recurrence relation f(x):
f(x)=1 where =1
f(x) = f((x+1)/2) +1 where x is odd
f(x) = f(x/2) +1 where x is even

This is what i did:

#include<stdio.h>

int func (int x)

int main()
{
	int x;
	printf("Please enter the value of x: ");
	scanf("%d", &x);

	int func=func(x);
	printf("%d",func) ;

	return 0;
}

int func (int x)
{
	if(x==1) 
		return 1;
	else if (x%2 ==1)
		return func ( (x+1)/2 ) + 1;
	else
		return func (x/2) + 1;
}

But it doesnt work.
Could you please kindly explain how a recursion work as well?
Thank youuuuu!

What doesn't work about it? All I see wrong with your code is a missing ";" after your function prototype.

Yeah Right
Semicolon at the end of the function declaration is missing

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.