FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
C-Plus-Plus/others/String Fibonacci.cpp at master · SelfCodeLearning/C-Plus-Plus · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SelfCodeLearning
/
C-Plus-Plus
Public
forked from
TheAlgorithms/C-Plus-Plus
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
C-Plus-Plus
/
others
/
String Fibonacci.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (70 loc) · 1.58 KB
Breadcrumbs
C-Plus-Plus
/
others
/
String Fibonacci.cpp
Copy path
File metadata and controls
83 lines (70 loc) · 1.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
This Programme returns the Nth fibonacci as a string.
//
The method used is manual addition with carry and placing it in a string which is called string addition
//
This makes it have no bounds or limits
#
include
<
iostream
>
#
include
<
string
>
using
namespace
std
;
string
add
(string a, string b)
{
string temp =
"
"
;
//
carry flag
int
carry =
0
;
//
fills up with zeros
while
((
int
)a.
length
() < (
int
)b.
length
())
{
a =
"
0
"
+ a;
}
//
fills up with zeros
while
((
int
)b.
length
() < (
int
)a.
length
())
{
b =
"
0
"
+ b;
}
//
adds the numbers a and b
for
(
int
i = a.
length
() -
1
; i >=
0
; i--)
{
char
val = (
char
)(((a[i] -
48
) + (b[i] -
48
)) +
48
+ carry);
if
(val >
57
)
{
carry =
1
;
val -=
10
;
}
else
{
carry =
0
;
}
temp = val + temp;
}
//
processes the carry flag
if
(carry ==
1
)
{
temp =
"
1
"
+ temp;
}
//
removes leading zeros.
while
(temp[
0
] ==
'
0
'
&& temp.
length
() >
1
)
{
temp = temp.
substr
(
1
);
}
return
temp;
}
void
fib_Accurate
(
long
long
n)
{
string tmp =
"
"
;
string fibMinus1 =
"
1
"
;
string fibMinus2 =
"
0
"
;
for
(
long
long
i =
0
; i < n; i++)
{
tmp =
add
(fibMinus1, fibMinus2);
fibMinus2 = fibMinus1;
fibMinus1 = tmp;
}
cout << fibMinus2;
}
int
main
()
{
int
n;
cout <<
"
Enter whatever number N you want to find the fibonacci of
\n
"
;
cin >> n;
cout << n <<
"
th Fibonacci is
\n
"
;
fib_Accurate
(n);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL