0% found this document useful (0 votes)
2 views

C++ Exercises

The document contains three C++ programs that generate Fibonacci numbers. The first program calculates and displays the 100th Fibonacci number, the second prints all Fibonacci numbers up to the 100th, and the third allows user input for the first two Fibonacci numbers and the desired position to calculate the nth Fibonacci number. Each program utilizes a loop to compute Fibonacci values iteratively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ Exercises

The document contains three C++ programs that generate Fibonacci numbers. The first program calculates and displays the 100th Fibonacci number, the second prints all Fibonacci numbers up to the 100th, and the third allows user input for the first two Fibonacci numbers and the desired position to calculate the nth Fibonacci number. Each program utilizes a loop to compute Fibonacci values iteratively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

I - The program that generated 100th Fibonacci values:

#include <iostream>

using namespace std;

int main() {

unsigned long pre1=0, pre2=1, counter=0, curr;

while(counter < 98){

curr = pre1 + pre2;

pre1 = pre2;

pre2 = curr;

counter++;

cout<<"The 100th Fibonacci is: "<<curr;

return 0;

II - The Fibonacci with all the values printed up to 100th:

#include <iostream>

using namespace std;

int main() {

unsigned long pre1=0, pre2=1, counter=0, curr;

while(counter < 98){

curr = pre1 + pre2;

pre1 = pre2;

pre2 = curr;

counter++;

cout<<curr<<" ";
}

return 0;

III – The following program prompts the user for 1st two values and the position of the nth Fibonacci
number. It then generate the nth Fibonacci value:

#include <iostream>

using namespace std;

int main() {

unsigned long pre1, pre2, n, counter=0, curr, i;

cout<<"Enter first two Fibonacci values and Fibonacci number:"<<endl;

cin>>pre1>>pre2>>n;

i = n - 2;

while(counter < i){

curr = pre1 + pre2;

pre1 = pre2;

pre2 = curr;

counter++;

cout<<"The "<<n<<"th"<<" Fibonacci value is: "<<curr;

return 0;

You might also like