---
title: "nth Fibonacci term prime check"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:fibonacci-nth-prime"
source: https://www.bctengineer.com/q/semester_1/computer_programming/fibonacci-nth-prime
site: BCT Engineering
---

# nth Fibonacci term prime check

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2074 Asoj, 8 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/fibonacci-nth-prime
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

Write a program in C to find out whether the nth term of the Fibonacci series is a prime number or not. Read the value of n from the user and display the result in the main function. Use separate user-defined functions to generate the nth Fibonacci term and to check whether a number is prime or not.

## Short answer

Generate the nth Fibonacci number (starting 0,1) iteratively by updating a and b. Check primality by trial division up to square root. For input n=8, the Fibonacci term is 13, which is prime. For n=7, term 8 is not prime.

## Answer

The Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, 21, ... with each term the sum of the previous two. The function `fib(n)` returns the nth term (term 1 = 0, term 2 = 1). The function `isprime()` returns 1 when a number is prime. `main()` reads n, calls both functions and displays the result.

```c
#include <stdio.h>
long fib(int n)
{
    long a = 0, b = 1, t;
    int i;
    if (n == 1) return 0;
    if (n == 2) return 1;
    for (i = 3; i <= n; i++)
    {
        t = a + b;
        a = b;
        b = t;
    }
    return b;
}
int isprime(long x)
{
    long i;
    if (x < 2) return 0;
    for (i = 2; i * i <= x; i++)
        if (x % i == 0)
            return 0;
    return 1;
}
int main(void)
{
    int n;
    long f;
    printf("Enter n: ");
    scanf("%d", &n);
    f = fib(n);
    printf("Term %d of the Fibonacci series = %ld\n", n, f);
    if (isprime(f))
        printf("%ld is a prime number\n", f);
    else
        printf("%ld is not a prime number\n", f);
    return 0;
}
```
Output for n = 7:
```text
Enter n: Term 7 of the Fibonacci series = 8
8 is not a prime number
```

Output for n = 8: `Term 8 of the Fibonacci series = 13` and `13 is a prime number`.

#### Explanation
- `fib()` keeps the last two terms in `a` and `b` and moves forward with `t = a + b`.
- `isprime()` rejects numbers below 2 and tests divisors from 2 up to √x.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. fib(n) computes?
   A. Recursion only
   B. Keeps last two terms, t=a+b
   C. Array
   D. Formula
   Answer: B. Iterative. (Key point: fib method)

2. Term 1 and 2?
   A. 1 and 1
   B. 0 and 0
   C. 0 and 1
   D. 1 and 2
   Answer: C. As defined. (Key point: Term)

3. isprime tests?
   A. 1 to x
   B. even only
   C. x/2
   D. Divisors 2 to sqrt(x)
   Answer: D. Optimization. (Key point: isprime)

4. n=7 gives?
   A. 8, not prime
   B. 13 prime
   C. 5 prime
   D. 7 prime
   Answer: A. Terms 0,1,1,2,3,5,8. (Key point: n=7)

## Related in Computer Programming

- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Significance of user defined functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-user-defined-functions) — Computer Programming, chapter 6
- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Check triangular number](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Atriangular-number) — Computer Programming, chapter 5

## Related in other subjects

- [Prime number program: procedural and OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprime-procedural-oop) — Object Oriented Programming, chapter 1
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Prime number checking processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-prime-check) — Embedded System, chapter 2
- [Convert user-defined type to basic type](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Auser-to-basic-conversion) — Object Oriented Programming, chapter 5
