Saved Bookmarks
| 1. |
Write a program to find the Nth term in this series. The value N in a positive integer that should be read from STDIN. The Nth term that is calculated by the program should be written to STDOUT. Other than the value of Nth term , no other characters / string or message should be written to STDOUT. For example, when N=14, the 14th term in the series is 17. So only the value 17 should be printed to STDOUT. |
|
Answer» Answer: In C Language: #include #define MAX 1000 void FIBONACCI(int n) { int i, T1 = 0, T2 = 1, nextTerm; for (i = 1; i<=n; i++) { nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; } printf("%d", t1); } void PRIME(int n) { int i, j, flag, count =0; for (i=2; i<=MAX; i++) { flag = 0; for (j=2; j { if(i%j == 0) { flag = 1; break; } } if (flag == 0) if(++count == n) { printf("%d", i); break; } } } int main() { int n; scanf("%d", &n); if(n%2 == 1) fibonacci (n/2 + 1); else prime(n/2); return 0; } |
|