Java

 

 

Introduction

In mathematics and computer science, number sequences play a significant role in pattern recognition and problem-solving. This program is designed to analyze and generate sequences like Fibonacci and Arithmetic Progression (AP). The goal is to understand these sequences’ structure and identify patterns to aid in further analysis or computation.

Objective

The primary objective of this program is to provide a simple tool for analyzing different types of number sequences, such as Fibonacci sequences and arithmetic progressions. By implementing this tool in Java, you can easily input the number of terms to generate and analyze the sequence behavior.

Java Code

import java.util.Scanner;

public class SequenceAnalyzer {

    // Method to generate Fibonacci sequence
    public static void generateFibonacci(int terms) {
        int a = 0, b = 1;
        System.out.println("Fibonacci Sequence:");
        for (int i = 1; i <= terms; i++) {
            System.out.print(a + " ");
            int next = a + b;
            a = b;
            b = next;
        }
        System.out.println();
    }

    // Method to generate Arithmetic Progression (AP) sequence
    public static void generateArithmeticProgression(int terms, int firstTerm, int commonDifference) {
        System.out.println("Arithmetic Progression (AP) Sequence:");
        for (int i = 0; i < terms; i++) {
            System.out.print(firstTerm + " ");
            firstTerm += commonDifference;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // User input for sequence choice
        System.out.println("Choose the sequence type:");
        System.out.println("1. Fibonacci");
        System.out.println("2. Arithmetic Progression (AP)");
        System.out.print("Enter your choice (1 or 2): ");
        int choice = scanner.nextInt();
        
        // User input for number of terms
        System.out.print("Enter the number of terms: ");
        int terms = scanner.nextInt();
        
        if (choice == 1) {
            // Fibonacci sequence
            generateFibonacci(terms);
        } else if (choice == 2) {
            // AP sequence
            System.out.print("Enter the first term: ");
            int firstTerm = scanner.nextInt();
            System.out.print("Enter the common difference: ");
            int commonDifference = scanner.nextInt();
            generateArithmeticProgression(terms, firstTerm, commonDifference);
        } else {
            System.out.println("Invalid choice! Please restart the program.");
        }
        
        scanner.close();
    }
}

Program Structure and Explanation

The program is designed to generate two types of number sequences: Fibonacci and Arithmetic Progression. It follows a clear structure, where the user selects the desired sequence type and inputs the required parameters to generate the sequence.

  • Fibonacci Sequence: Starts with 0 and 1, and each subsequent number is the sum of the previous two numbers.
  • Arithmetic Progression (AP): Starts with a user-defined first term and progresses with a constant difference between consecutive terms.

How to Run the Program:

  1. Open a text editor (e.g., Notepad++) and paste the Java code into a new file.
  2. Save the file as SequenceAnalyzer.java.
  3. Open a terminal or command prompt and navigate to the directory where the file is saved.
  4. Compile the Java program by running: javac SequenceAnalyzer.java
  5. Run the program by typing: java SequenceAnalyzer
  6. Follow the prompts to choose the sequence type and input the necessary values.
© 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 :)