CSC115 ยท 2080 ยท 10 marks
Define structure and nested structure. Write a program 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 function to (i) generate nth term and (ii) to check wheth
Define structure and nested structure. Write a program to find out whether the $n^{th}$ 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 function to (i) generate $n^{th}$ term and (ii) to check whether that number is prime or not.[3+7]
Quick answer
A structure is a user-defined data type in C that allows grouping of variables of different data types under a single name. Each variable inside a structure is called a member or field.
Structure, Nested Structure, and Fibonacci Prime Program
(a) Definitions
Structure
Syntax:
struct structure_name {
data_type member1;
data_type member2;
...
};
Example:
struct Student {
int roll;
char name[20];
float marks;
};
Nested Structure
A nested structure is a structure that contains another structure as its member. That is, one structure is defined or declared inside another structure.
Syntax:
struct Inner {
data_type member1;
};
struct Outer {
data_type member;
struct Inner obj; /* nested structure member */
};
Example:
struct Date {
int day;
int month;
int year;
};
struct Student {
int roll;
char name[20];
struct Date dob; /* nested structure */
};
Here, struct Date is nested inside struct Student. The member dob is accessed as s.dob.day, s.dob.month, etc.
(b) Program
Problem Analysis
- Read
nfrom the user inmain(). - Call function
fibonacci(n)to find the nth term of the Fibonacci series. - Call function
isPrime(num)to check whether that term is prime or not. - Display the result in
main().
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, ... The 1st term = 0, 2nd term = 1, 3rd term = 1, and so on.
Complete Program
#include <stdio.h>
#include <conio.h>
/* Function to generate the nth term of Fibonacci series */
int fibonacci(int n)
{
int first = 0, second = 1, next, i;
if (n == 1)
return 0;
if (n == 2)
return 1;
for (i = 3; i <= n; i++)
{
next = first + second;
first = second;
second = next;
}
return next;
}
/* Function to check whether a number is prime or not */
/* Returns 1 if prime, 0 if not prime */
int isPrime(int num)
{
int i;
if (num <= 1)
return 0; /* 0 and 1 are not prime */
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
return 0; /* divisible, so not prime */
}
return 1; /* prime */
}
/* Main function */
void main()
{
int n, term, result;
clrscr();
printf("Enter the value of n: ");
scanf("%d", &n);
/* Generate nth Fibonacci term */
term = fibonacci(n);
printf("\nThe %dth term of Fibonacci series is: %d\n", n, term);
/* Check if the term is prime */
result = isPrime(term);
if (result == 1)
printf("%d is a PRIME number.\n", term);
else
printf("%d is NOT a prime number.\n", term);
getch();
}
Sample Output
Enter the value of n: 7
The 7th term of Fibonacci series is: 8
8 is NOT a prime number.
Enter the value of n: 5
The 5th term of Fibonacci series is: 3
3 is a PRIME number.
Fibonacci Series Reference Table
| n | Fibonacci Term |
|---|---|
| 1 | 0 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 3 |
| 6 | 5 |
| 7 | 8 |
| 8 | 13 |
Key Points
| Function | Purpose | Return Type |
|---|---|---|
fibonacci(n) | Returns the nth Fibonacci term | int |
isPrime(num) | Returns 1 if prime, 0 if not | int |
main() | Reads input, calls both functions, displays result | void |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.