We consider the function \(f(x)=\frac{1}{1+x^2}\) and the series \(u_0=1\) and \(u_{n+1}=f(u_n)\)
Write a program that calculates and displays the successive terms of \(u_n\) until \(|u_{n+1}-u_n|\) is less than \(\epsilon=10^{-8}\).
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main()
{
double u,eps,ua,diff; int n;
u=1.; eps=1.e-8; n=0;
do
{
printf("u(%d)=%20.15lg\n",n,u);
ua=u;
u=1./(1.+u*u);
n++;
diff=fabs(u-ua);
}while(diff>=eps);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting using the radix sort algorithm