Hi there guys.
Literally I have been asked this question;
"The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, … ; the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms – i.e., Fib[n] = Fib[n – 1] + Fib[n – 2]. Using this information, write a C++ program that calculates the nth number in a Fibonacci sequence, where the user enters n in the program interactively. For example, if n = 6, the program should display the value 5."
I have created the program, but I want to give the User an option to either go back to the start of the program, or to quit it. i.e. I don't want them to have to continually click 'Run'. I know this is something to do with the 'Do While' Loop, but I am a bit of a novice and not really sure how to go about it/code it.
Any thoughts on my question and/or examples/walkthroughs would be much appreciated, just trying to get my head round it. Any suggestions to improve my code so far is also welcome. Thank you! :)
Here is my code so far.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int N;
cout<<"This program is designed to give the user any value of the Fibonacci Sequence that they desire, provided the number is a positive integer.";//Tell user what the program does
cout<<"\n\nThe formula of the Fibonacci Sequence is; Fib[N] = Fib[N – 1] + Fib[N – 2]\n\n"; //Declare the Formula for the User
cout<<"Enter a value for N, then press Enter:"; //Declare Value that the User wants to see
cin>>N;//Enter the Number
if (N>1) {
cout<<"\nFor your value of N, \nFib[N]= "<<((pow(((1+(pow(5, 0.5)))), N))-(pow(((1-(pow(5, 0.5)))), N)))*(pow((pow(2.0, N)*(pow(5, 0.5))), -1));//Mathematic formula for Fibonacci Sequence
}
if (N<0) {
cout<<"\n\nThe value N must be a POSITIVE integer, i.e. N > 0"; //Confirm that N must be a positive integer. Loop.
}
if (N>100) {
cout<<"\n\nThe value for N must be less than 100, i.e. N < 100. N must be between 0 - 100.";//Confirm that N must be less than 100. Loop.
}
if (N==0) {
cout<<"\n\nFor your value of N, \nFib[0] = 0"; //Value will remain constant throughout, cannot be caculated through formula. Loop.
}
if (N==1) {
cout<<"\n\nFor your value of N. \nFib[1]=1";//Value will remain constant throughout, cannot be caculated through formula. Loop.
}
return 0;
}
PS Sorry for it all being in Green, just the way it came out. Obviously not displayed like that in the actually program.