Answer to Question #142456 in C for NAVANIT KRISHNA

Question #142456
) Write function definition (recursive as well as non-recursive) to generate the following series up to
n numbers. The first two numbers are known.
1 2 6 16 44 120….
1
Expert's answer
2020-11-04T17:54:55-0500
#include <stdio.h>

void non_recursive(int v1, int v2, int n)
{
	printf("%d %d ", v1, v2);
	
	for(int i = 0; i < n-2; ++i)
	{
		int tmp = 2 * (v1 + v2);
		v1 = v2;
		v2 = tmp;
		printf("%d ", tmp);
	}
}

void recursive(int v1, int v2, int n)
{
	printf("%d ", v1);

	if(--n > 0)
	{
		recursive(v2, 2 * (v1 + v2), n);
	}
}

int main()
{
	const int n = 6;
	
	non_recursive(1, 2, n);
	printf("\n");
	
	recursive(1, 2, n);
	printf("\n");
    
    return 0;
}

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