Question
c++ fibonacci code using loops
Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21 Note that the first Fibonacci number is 1, F(1) = 1 The second Fibonac
1 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

int main()
{
int N, M;
int sum = 0;
int fib1, fib2, fib3;
float average;

cout << "JOSEPHINE" << endl;
  
cout << "Part 1: Sum of first N positive integers:" << endl;
cout << "Enter N: " ;
cin >> N;
// For loop is used to vary the value of number from 1 to N
for(int number= 1; number<=N; number++)
{
// Keep adding number to the previous value of sum in every cycle of for loop
sum = sum + number;
}
cout << "Sum of first " << N << " positive integer numbers is " << sum << "." << endl << endl;
  
cout << "Part 2: Print first N Fibonacci numbers, their sum and average:" << endl;
cout << "Enter N: " ;
cin >> N;
// Assign the first two numbers of Fibonacci series in fib1 and fib2
fib1 = 1;
fib2 = 1;
// add the first two numbers to get the sum
sum = fib1 + fib2;
// print the first two numbers
cout << "First " << N << " Fibonacci numbers are:" << endl;
cout << "F(1) = " << fib1 << endl;
cout << "F(2) = " << fib2 << endl;
// for loop is used to generate fibonacci series from third term as
// first two terms are printed out of loop
for(int i= 3; i<=N; i++)
{
// Generate every third term by adding previous two terms and display
fib3 = fib1 + fib2;
cout << "F(" << i << ") = " << fib3 << endl;
// add the generated third term to sum
sum = sum + fib3;
// assign the third term to fib2 and second term to fib1
fib1 = fib2;
fib2 = fib3;
}
// find the average and print the sum and average
average = ((float)sum/N);
cout << "Sum of the first " << N << " Fibonacci numbers = " << sum << endl;
cout << "Average of the first " << N << " Fibonacci numbers = " << average << endl << endl;
  
  cout << "Part 3: Generate N Fibonacci numbers but print every M term:" << endl;
cout << "Enter N: " ;
cin >> N;
cout << "Enter M: " ;
cin >> M;
cout << N << " Fibonacci numbers printed with sequence number multiple of " << M << ":" << endl;
fib1 = 1;
fib2 = 1;
// print the first two numbers if term is 1
if(M == 1)
{
cout << "F(1) = " << fib1 << endl;
cout << "F(2) = " << fib2 << endl;
}
// print the second number if term is 2
else if(M == 2)
{
cout << "F(2) = " << fib2 << endl;
}
for(int i= 3; i<=N; i++)
{
fib3 = fib1 + fib2;
// check if i is divisible by M. If so, print the i(th) terms
if(i%M == 0)
cout << "F(" << i << ") = " << fib3 << endl;
fib1 = fib2;
fib2 = fib3;
}
return 0;
}

=====================================================================================

#include <iostream> using namespace std; int main() { int N, M; int sum = 0; int fibi, fib2, fib3; float average; cout << JO// add the first two numbers to get the sum sum = fib1 + fib2; // print the first two numbers cout << First << N << Fibo<< : << endl; cin >> M; cout <<N << Fibonacci numbers printed with sequence number multiple of << M fib1 = 1; fib2 = 1;

=============================================================================================

Sample output:

JOSEPHINE Part 1: Sum of first N positive integers: Enter N: 3 Sum of first 3 positive integer numbers is 6. Part 2: Print fi

Add a comment
Know the answer?
Add Answer to:
c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence...

    Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence of numbers where each number is represented by the sum of the two preceding numbers, starting with 0 and 1: 0, 1, 1, 2, 3, 5, 8, etc Write a program that repeatedly prompts the user for a positive integer and prints out whether or not that integer is a Fibonacci number. The program terminates when-I is entered. Create a method with the following...

  • The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...

    The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The next number is found by adding up the two numbers before it. For example, the 2 is found by adding the two numbers before it (1+1). The 3 is found by adding the two numbers before it (1+2). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called...

  • C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a...

    C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a simple pattern: 1,1, 2, 3, 5, 8, 13, 21, 34, 55, By definition, the first two numbers are 1 and 1, and each subsequent number is the sum of the previous two. For your program, you will ask the user to enter a number, n, and then calculate and display the values of the Fibonacci sequence to the nth position. ==sample output== Enter an...

  • Write a C program to compute and print Fibonacci values for some integers. You can assume...

    Write a C program to compute and print Fibonacci values for some integers. You can assume that all input numbers are positive integers. The Fibonacci numbers are defined as follows: The first two Fibonacci numbers are 1 and 1. Any Fibonacci number after the first two numbers is the sum of its two predecessors. The Fibonacci numbers can be listed as the following sequence:         1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... It is clear...

  • Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the...

    Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the linear recurrence equation Fn F-1 F-2 where F F2 1 and by convention Fo 0. For example, the first 8 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21. (a) For a given n, compute the nth Fibonnaci number using a for loop (b) For a given n, compute the nth Fibonnaci number using a while loop Print the 15th Fibonacci number...

  • 2. The Fibonacci numbers are defined by the sequence: f = 1 f2 = 1 fo=fni...

    2. The Fibonacci numbers are defined by the sequence: f = 1 f2 = 1 fo=fni + 2 Implement a program that prompts the user for an integer, n, and prints all the Fibonacci numbers, up to the nth Fibonacci number. Use n=10. Show a sample output with the expected results. Output: Enter a number: 100 number Fib 89

  • Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formal...

    Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formally, it can be expressed as: fib0-0 fibl-1 fibn-fibn-1+fibn-2 Write a multithreaded program that generates the Fibonacci sequence. This program should work as follows: On the command line, the user will enter the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in...

  • use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can...

    use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....

  • In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.......

    In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.... In mathematical notation the sequence Fn of Fibonacci number is defined by the following recurrence relation: Fn=Fn-1+Fn-2 With the initial values of F0=0 and F1=1. Thus, the next number in the series is the sum of the previous two numbers. Write a program that asks the user for a positive integer N and generate the Nth Fibonacci number. Your main function should handle user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT