1
- #include <stdio.h>
1
+ /**
2
+ * @file
3
+ * @brief Program to print the nth term of the Fibonacci series.
4
+ * @details
5
+ * Fibonacci series generally starts from 0 and 1. Every next term in
6
+ * the series is equal to the sum of the two preceding terms.
7
+ * For further info: https://en.wikipedia.org/wiki/Fibonacci_sequence
8
+ *
9
+ * @author [Luiz Carlos Aguiar C](https://github.com/IKuuhakuI)
10
+ * @author [Niranjan](https://github.com/niranjank2022)
11
+ */
2
12
3
- // Fibonnacci function
4
- int fib (int number )
13
+ #include <assert.h> /// for assert()
14
+ #include <errno.h> /// for errno - to determine whether there is an error while using strtol()
15
+ #include <stdio.h> /// for input, output
16
+ #include <stdlib.h> /// for exit() - to exit the program
17
+ #include <time.h> /// to calculate time taken by fib()
18
+ /**
19
+ * @brief Determines the nth Fibonacci term
20
+ * @param number - n in "nth term" and it can't be negative as well as zero
21
+ * @return nth term in unsigned type
22
+ * @warning
23
+ * Only till 47th and 48th fibonacci element can be stored in `int` and
24
+ * `unsigned int` respectively (takes more than 20 seconds to print)
25
+ */
26
+ unsigned int fib (int number )
5
27
{
6
- if (number == 1 || number == 2 )
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 )
7
40
return 1 ;
8
- else
9
- return fib (number - 1 ) + fib (number - 2 );
41
+
42
+ // Recursive call to the function
43
+ return fib (number - 1 ) + fib (number - 2 );
10
44
}
11
45
46
+ /**
47
+ * @brief Get the input from the user
48
+ * @return valid argument to the fibonacci function
49
+ */
50
+ int getInput (void )
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
+ }
87
+
88
+ /**
89
+ * @brief self-test implementation
90
+ * @return void
91
+ */
92
+ static void test ()
93
+ {
94
+ assert (fib (5 ) == 3 );
95
+ assert (fib (2 ) == 1 );
96
+ assert (fib (9 ) == 21 );
97
+ }
98
+
99
+ /**
100
+ * @brief Main function
101
+ * @return 0 on exit
102
+ */
12
103
int main ()
13
104
{
14
- int number ;
105
+ // Performing the test
106
+ test ();
107
+ printf ("Tests passed...\n" );
15
108
16
- // Asks for the number that is in n position in Fibonnacci sequence
17
- printf ("Number: " );
18
- scanf ("%d" , & number );
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" );
19
114
20
- printf ("%d \n" , fib (number ));
115
+ int number = getInput ();
116
+ clock_t start , end ;
21
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 );
22
123
return 0 ;
23
- }
124
+ }
0 commit comments