#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;
int main()
{
unsigned num1 = 0, num2 = 1, nextNum = 0;
int k;
// taking input
while (1) {
cout << "k=";
cin >> k;
if (k > 1) {
break;
}
else {
// going to take input again
cout << "k should be > 1" << endl;
continue;
}
}
for ( int i = 1; i <= k; ++i) {
// first fibonacci number 0
if (i == 1) {
cout << num1 << " ";
continue;
}
// second fibonacci number 1
if (i == 2) {
cout << num2 << " ";
continue;
}
// for >=third number
nextNum = num1 + num2;
num1 = num2;
num2 = nextNum;
if (nextNum < k) {
cout << nextNum << " ";
}
}
return 0;
}
#include <cmath>
using std::cin;
using std::cout;
using std::endl;
int main()
{
unsigned num1 = 0, num2 = 1, nextNum = 0;
int k;
// taking input
while (1) {
cout << "k=";
cin >> k;
if (k > 1) {
break;
}
else {
// going to take input again
cout << "k should be > 1" << endl;
continue;
}
}
for ( int i = 1; i <= k; ++i) {
// first fibonacci number 0
if (i == 1) {
cout << num1 << " ";
continue;
}
// second fibonacci number 1
if (i == 2) {
cout << num2 << " ";
continue;
}
// for >=third number
nextNum = num1 + num2;
num1 = num2;
num2 = nextNum;
if (nextNum < k) {
cout << nextNum << " ";
}
}
return 0;
}
No comments:
Post a Comment