Introduction
Number sequences like Fibonacci and Arithmetic Progression (AP) are important concepts in mathematics and programming. A Fibonacci sequence is generated by adding the two preceding numbers to get the next one, starting from 0 and 1. On the other hand, an arithmetic progression consists of numbers in a sequence where the difference between consecutive terms is constant.
In this program, we will analyze two common number sequences: Fibonacci and Arithmetic Progression. The objective of this program is to calculate and display terms from these sequences based on user input for the number of terms and the difference (for AP). This will help understand the structure and behavior of these sequences, which are widely used in various fields of science, engineering, and computing.
Objective
The objective of this program is to:
- Generate the Fibonacci sequence up to a given number of terms.
- Generate an Arithmetic Progression (AP) with a specified difference between terms.
- Allow users to input the number of terms and difference for both sequences.
Code Implementation in C++
#include
using namespace std;
// Function to generate Fibonacci sequence
void generateFibonacci(int terms) {
int a = 0, b = 1, c;
cout << "Fibonacci Sequence: " << a << " " << b << " ";
for (int i = 3; i <= terms; i++) {
c = a + b;
cout << c << " ";
a = b;
b = c;
}
cout << endl;
}
// Function to generate Arithmetic Progression (AP)
void generateAP(int terms, int difference, int firstTerm = 0) {
cout << "Arithmetic Progression: ";
for (int i = 0; i < terms; i++) {
cout << firstTerm + i * difference << " ";
}
cout << endl;
}
int main() {
int choice, terms, difference;
cout << "Choose a sequence to generate:\n";
cout << "1. Fibonacci Sequence\n";
cout << "2. Arithmetic Progression\n";
cout << "Enter your choice (1 or 2): "; cin >> choice;
cout << "Enter the number of terms to generate: "; cin >> terms;
if (choice == 1) {
generateFibonacci(terms);
} else if (choice == 2) {
cout << "Enter the common difference for AP: "; cin >> difference;
generateAP(terms, difference);
} else {
cout << "Invalid choice!" << endl;
}
return 0;
}
Program Explanation
The program provides the user with two options: generating the Fibonacci sequence or an Arithmetic Progression (AP). The user can select the desired sequence type and input the number of terms they want to generate. For the AP, they also input the common difference between terms.
Structure of the Program:
- Function generateFibonacci(int terms): This function generates the Fibonacci sequence by iteratively adding the two previous numbers to form the next number, starting with 0 and 1.
- Function generateAP(int terms, int difference, int firstTerm): This function generates the Arithmetic Progression. The default first term is set to 0, but it can be modified. The function uses the common difference to generate each term in the sequence.
- In the main function: The user is prompted to select between Fibonacci and AP. Based on the selection, the corresponding function is called, and the user is asked to input the necessary parameters like the number of terms and, for AP, the common difference.
How to Run the Program:
To run this C++ program, follow these steps:
- Open a text editor or an Integrated Development Environment (IDE) like Code::Blocks, Visual Studio, or any C++ compiler.
- Copy and paste the provided C++ code into your editor.
- Save the file with a .cpp extension (e.g., number_sequence_analyzer.cpp).
- Compile and run the program.
- Follow the on-screen prompts to select a sequence and input the required values.