Series of numbers

 

A series of numbers is as follows:
1, 1, 1, 3, 5, 9, 17, 31, 57, 105, . . . , An = An-1 + An-2 + An-3
Write a Computer Program to compute An for the user input n, where A1 = A2 = A3 = 1.

Program:

#include<stdio.h>
int main()
{
             int num;
             int A1=1,A2=1,A3=1;                 /* initialize to start the series */
             int A4,i;
             printf("enter num: ");
             scanf("%d",&num);
             for(i=4;i<=num;i++)                      /* starting from 4th term  */
            {
                       A4=A1+A2+A3;                 /* next term in the series */
                       A1=A2;
                       A2=A3;
                       A3=A4;
             }
             printf("%dth number in the series= %d",num,A4);
             return 0;
}

Sample input:

 enter num: 10

Output:

10th number in the series= 105

No comments: