Introduction:
In mathematics and computer science, analyzing number sequences like the Fibonacci series or Arithmetic Progression (AP) is an essential part of problem-solving. This program will help you analyze and generate these sequences. Understanding how these sequences work can help develop a better understanding of recursive and iterative programming approaches. In this guide, we will focus on creating a Number Sequence Analyzer that works with both Fibonacci and Arithmetic Progression sequences in the C programming language.

Objective:
The goal of this program is to allow users to choose between analyzing a Fibonacci sequence or an Arithmetic Progression. The program will then calculate and display the sequence based on user input for parameters such as the number of terms and the common difference (for AP) or simply the number of terms for Fibonacci.

Code Implementation

#include 

// Function to generate Fibonacci sequence
void generateFibonacci(int n) {
    int a = 0, b = 1, c;
    printf("Fibonacci Sequence: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", a);
        c = a + b;
        a = b;
        b = c;
    }
    printf("\n");
}

// Function to generate Arithmetic Progression sequence
void generateAP(int firstTerm, int difference, int n) {
    printf("Arithmetic Progression: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", firstTerm + i * difference);
    }
    printf("\n");
}

int main() {
    int choice, n, firstTerm, difference;

    // User menu
    printf("Number Sequence Analyzer\n");
    printf("1. Fibonacci Sequence\n");
    printf("2. Arithmetic Progression Sequence\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    if (choice == 1) {
        // Fibonacci sequence generation
        printf("Enter the number of terms for Fibonacci Sequence: ");
        scanf("%d", &n);
        generateFibonacci(n);
    } 
    else if (choice == 2) {
        // Arithmetic progression sequence generation
        printf("Enter the first term of the Arithmetic Progression: ");
        scanf("%d", &firstTerm);
        printf("Enter the common difference of the Arithmetic Progression: ");
        scanf("%d", &difference);
        printf("Enter the number of terms for Arithmetic Progression: ");
        scanf("%d", &n);
        generateAP(firstTerm, difference, n);
    } 
    else {
        printf("Invalid choice!\n");
    }

    return 0;
}

Explanation of the Program Structure

This C program starts by offering a simple menu to the user. The user can choose to generate either a Fibonacci sequence or an Arithmetic Progression sequence. Here’s the breakdown of the program structure:

  • Function Definitions:
    • generateFibonacci(int n): This function calculates and prints the Fibonacci sequence for the given number of terms ‘n’. It uses two variables ‘a’ and ‘b’ to store the Fibonacci numbers, iteratively calculating the next term in the sequence.
    • generateAP(int firstTerm, int difference, int n): This function calculates and prints the Arithmetic Progression sequence. It takes the first term, common difference, and the number of terms ‘n’ to generate the sequence.
  • Main Function: This part of the program prompts the user to input their choice, along with the necessary parameters for the sequence they wish to generate. Depending on the user’s choice, it calls the corresponding function to display the sequence.

How to Run the Program

To run the program, follow these steps:

    1. Copy the provided C code into a text editor.
    2. Save the file with a .c extension (e.g., sequence_analyzer.c).
    3. Open a terminal or command prompt.
    4. Navigate to the directory where the file is saved.
    5. Compile the C program using a C compiler (e.g., GCC). For example:
gcc sequence_analyzer.c -o sequence_analyzer
    1. Run the program:
./sequence_analyzer
  1. Follow the on-screen prompts to choose and generate the desired number sequence.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)