Write a program that reads a positive integer n and then displays the nth term of the following series :

\(\Big\{\begin{array}{c c c} U_0 & = & 1 \\ U_n & = & V_{n-1}+1 \end{array}\)

\(\Big\{\begin{array}{c c c} V_0 & = & 0 \\ V_n & = & 2 \times U_{n-1} \end{array}\)

Running example:


Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>

void main()
{
	int n, i, u=1, v=0, un=1, vn=0;
	do {
		printf("Enter n:");
		scanf("%d", &n);
	} while (n < 0);
	for(i=1;i<=n;i++)
	{
		un=v+1;
		vn=2*u;
		u=un;
		v=vn;
	}
	printf("U%d=%d\nV%d=%d\n", n, un, n, vn);
	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Checking whether elements of 2 BSTs are the same using recursion