- Write a program that takes an integer n and displays the value of $$U_n$$ respecting:
$$U_1=1$$ ;
$$U_n=\sum_{i=1}^{n-1}\frac{U_i}{(i-1)!}$$ for n>2 ; - Write a program that asks the user to enter an integer a and then prints the series elements from $$U_1$$ to $$U_a$$.
Difficulty level
Video recording
This exercise is mostly suitable for students
#include<stdio.h>
int main()
{
int n, i, j;
int fact=1;
double u1,u2;
u1=1;
u2=0;
do{
printf("Enter n: ");
scanf("%d",&n);
}while(n<=0);
for(i=2;i<=n;i++)
{
u2 += u1/fact;
fact*= (i-1) ;
u1 = u2;
}
printf("U%d=%.4lf\n",n,u1);
return 0;
}
#include<stdio.h>
int main()
{
int n, i, j;
int fact=1;
double u1,u2;
u1=1;
u2=0;
do{
printf("Enter n: ");
scanf("%d",&n);
}while(n<=0);
for(i=2;i<=n;i++)
{
u2 += u1/fact;
fact*= (i-1) ;
u1 = u2;
printf("U%d=%.4lf\n",i,u1);
}
return 0;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Binary tree rotations