Python/C/C++/JAVA

Moderate Practice Programs with Code and Concept

By D.S

Print Pyramid using Star*

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 rows, i, j;

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

    for (i = 1; i <= rows; ++i) {
        for (j = 1; j <= i; ++j) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:-
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *

(b.) C++ Code

#include <iostream>
using namespace std;

int main() {
    int rows;

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (int i = 1; i <= rows; ++i) {
        for (int j = 1; j <= i; ++j) {
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}
Output:-
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *

(c.) Python Code

def main():
    rows = int(input("Enter the number of rows: "))

    for i in range(1, rows + 1):
        print("* " * i)

if __name__ == "__main__":
    main()
Output:-
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *

(d.) Java Code

import java.util.Scanner;

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

        for (int i = 1; i <= rows; ++i) {
            for (int j = 1; j <= i; ++j) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}
Output:-
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *