Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string findSum(string str1, string str2)
- {
- if (str1.length() > str2.length())
- {
- swap(str1, str2);
- }
- string str = "";
- long long int n1 = str1.length(), n2 = str2.length(), carry = 0;
- reverse(str1.begin(), str1.end());
- reverse(str2.begin(), str2.end());
- for (long long int i = 0; i < n1; i++)
- {
- long long int sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
- str.push_back(sum % 10 + '0');
- carry = sum / 10;
- }
- for (long long int i = n1; i < n2; i++)
- {
- long long int sum = ((str2[i] - '0') + carry);
- str.push_back(sum % 10 + '0');
- carry = sum / 10;
- }
- if (carry)
- {
- str.push_back(carry + '0');
- }
- reverse(str.begin(), str.end());
- return str;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- long long int count;
- while (cin >> count && count != EOF)
- {
- string a, b, ans;
- a = "0";
- b = "1";
- for (long long int i = 3; i <= count + 1; i++)
- {
- ans = b;
- b = findSum(a, b);
- a = ans;
- }
- cout << "The Fibonacci number for " << count << " is " << b << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement