Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
fibonacci.c File Reference

Program to print the nth term of the Fibonacci series. More...

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Include dependency graph for fibonacci.c:

Functions

unsigned int fib (int number)
 for assert() for errno - to determine whether there is an error while using strtol()
 
int getInput (void)
 Get the input from the user.
 
static void test ()
 self-test implementation
 
int main ()
 Main function.
 

Detailed Description

Program to print the nth term of the Fibonacci series.

Fibonacci series generally starts from 0 and 1. Every next term in the series is equal to the sum of the two preceding terms. For further info: https://en.wikipedia.org/wiki/Fibonacci_sequence

Author
Luiz Carlos Aguiar C
Niranjan

Function Documentation

◆ fib()

unsigned int fib ( int  number)

for assert() for errno - to determine whether there is an error while using strtol()

for input, output for exit() - to exit the program to calculate time taken by fib()

Determines the nth Fibonacci term

Parameters
number- n in "nth term" and it can't be negative as well as zero
Returns
nth term in unsigned type
Warning
Only till 47th and 48th fibonacci element can be stored in int and unsigned int respectively (takes more than 20 seconds to print)
27{
28 // Check for negative integers
29 if (number <= 0)
30 {
31 fprintf(stderr, "Illegal Argument Is Passed!\n");
32 exit(EXIT_FAILURE);
33 }
34
35 // Base conditions
36 if (number == 1)
37 return 0;
38
39 if (number == 2)
40 return 1;
41
42 // Recursive call to the function
43 return fib(number - 1) + fib(number - 2);
44}
unsigned int fib(int number)
for assert() for errno - to determine whether there is an error while using strtol()
Definition fibonacci.c:26
Here is the call graph for this function:

◆ getInput()

int getInput ( void  )

Get the input from the user.

Returns
valid argument to the fibonacci function
51{
52 int num, excess_len;
53 char buffer[3], *endPtr;
54
55 while (1)
56 { // Repeat until a valid number is entered
57 printf("Please enter a valid number:");
58 fgets(buffer, 3, stdin); // Inputs the value from user
59
60 excess_len = 0;
61 if (!(buffer[0] == '\n' ||
62 buffer[1] == '\n' ||
63 buffer[2] == '\n')) {
64 while (getchar() != '\n') excess_len++;
65 }
66
67 num = strtol(buffer, &endPtr,
68 10); // Attempts to convert the string to integer
69
70 // Checking the input
71 if ( // The number is too large
72 (excess_len > 0 || num > 48) ||
73 // Characters other than digits are included in the input
74 (*endPtr != '\0' && *endPtr != '\n') ||
75 // No characters are entered
76 endPtr == buffer)
77 {
78 continue;
79 }
80
81 break;
82 }
83
84 printf("\nEntered digit: %d (it might take sometime)\n", num);
85 return num;
86}
struct used to store character in certain times
Definition min_printf.h:35

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
104{
105 // Performing the test
106 test();
107 printf("Tests passed...\n");
108
109 // Getting n
110 printf(
111 "Enter n to find nth fibonacci element...\n"
112 "Note: You would be asked to enter input until valid number ( less "
113 "than or equal to 48 ) is entered.\n");
114
115 int number = getInput();
116 clock_t start, end;
117
118 start = clock();
119 printf("Fibonacci element %d is %u ", number, fib(number));
120 end = clock();
121
122 printf("in %.3f seconds.\n", ((double)(end - start)) / CLOCKS_PER_SEC );
123 return 0;
124}
static void test()
self-test implementation
Definition fibonacci.c:92
int getInput(void)
Get the input from the user.
Definition fibonacci.c:50
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

self-test implementation

Returns
void
93{
94 assert(fib(5) == 3);
95 assert(fib(2) == 1);
96 assert(fib(9) == 21);
97}
Here is the call graph for this function: