Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Fibonacci Sequence

If you want to write a program that displays the Fibonacci sequence, it's actually not too complicated. First off, you'll need to understand what the sequence is - essentially, it's a series of numbers where each number after the first two is the sum of the two preceding ones (e.g. 1, 1, 2, 3, 5, 8...). Once you've got that down pat, you can start coding. Typically this involves using loops and variables to generate each successive value in the sequence until your program reaches whatever endpoint you've specified (either a certain number of iterations or a maximum value for the generated numbers). Whether you're working in a language like Python or C++, there are plenty of resources out there with sample code and tutorials to help guide you through the process. All in all, writing a Fibonacci program can be a fun challenge that pays off with some nifty math-based visualizations!

(a.) C Code

#include <stdio.h>

    int main() {
        int n, first = 0, second = 1, next, c;

        printf("Enter the number of terms: ");
        scanf("%d", &n);

        printf("Fibonacci Sequence: ");

        for (c = 0; c < n; c++) {
            if (c <= 1)
                next = c;
            else {
                next = first + second;
                first = second;
                second = next;
            }
            printf("%d, ", next);
        }

        return 0;
    }
Output:-
Enter the number of terms: 10
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

(b.) C++ Code

#include <<iostream>

    using namespace std;

    int main() {
        int n, first = 0, second = 1, next;

        cout << "Enter the number of terms: ";
        cin >> n;

        cout << "Fibonacci Sequence: ";

        for (int c = 0; c < n; c++) {
            if (c <= 1)
                next = c;
            else {
                next = first + second;
                first = second;
                second = next;
            }
            cout << next << ", ";
        }

        return 0;
    }
Output:-
Enter the number of terms: 10
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

(c.) Python Code

def fibonacci(n):
        fib = [0, 1]
        for i in range(2, n):
            fib.append(fib[i - 1] + fib[i - 2])
        return fib

    n = int(input("Enter the number of terms: "))
    print("Fibonacci Sequence:", fibonacci(n))
Output:-
Enter the number of terms: 10
Fibonacci Sequence: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

(d.) Java Code

import java.util.Scanner;

    public class Fibonacci {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter the number of terms: ");
            int n = scanner.nextInt();

            int first = 0, second = 1;
            System.out.print("Fibonacci Sequence: ");
            for (int i = 1; i <= n; ++i) {
                System.out.print(first + ", ");
                int next = first + second;
                first = second;
                second = next;
            }
        }
    }
Output:-
Enter the number of terms: 10
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,