Find the Nth term for the given series
Write a C program to find the Nth term for the given series
1,2,3,5,8,13,21.....
whose first term is 1 and second term is 2 and Nth term will be the sum of term(N-1) and term (N-2)
Explanation :
term 1 = 1
term 2 = 2
term N = term(N-1) + term(N-2)
Sample input : 15
Sample output : 987
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <stdio.h> int main() { int term1=1,term2=2,sum=0; int itr,num; scanf("%d",&num); if(num==1 || num==2) // N= 1 or 2 printf("%d",num); else // for N above 2 { for(itr=3;itr<=num;itr++) { sum=term1+term2; term1=term2; term2=sum; } printf("%d",sum); } return 0; } |
OUTPUT :
0 comments