1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
Problem statement:- Program to print the Fibonacci Half Pyramid Pattern.
Data requirement:-
Input Data:- row_size
Output Data:- c
Additional Data:-out, in or inn(for python), a, b
Program in C
Here is the source code of the C Program to print the Fibonacci Half Pyramid Pattern.
Code:
#include <stdio.h>
int main()
{
int out, in, a = 0, b = 1, c = a + b;
printf("Enter the row size:");
int row_size;
scanf("%d", &row_size);
for (out = 1; out <= row_size; out++)
{
for (in = 1; in <= out; in++)
{
printf("%d ", c);
c = a + b;
a = b;
b = c;
}
printf("\n");
}
}
Enter the row size:5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
Program in C++
Here is the source code of the C++ Program to print the Fibonacci Half Pyramid Pattern.
Code:
#include <iostream>
using namespace std;
int main()
{
int out, in, a = 0, b = 1, c = a + b;
cout << "Enter the row size :";
int row_size;
cin >> row_size;
for (out = 1; out <= row_size; out++)
{
for (in = 1; in <= out; in++)
{
cout << c << " ";
c = a + b;
a = b;
b = c;
}
cout << "\n";
}
}
Enter the row size:4
1
1 2
3 5 8
13 21 34 55
Program in Java
Here is the source code of the Java Program to print the Fibonacci Half Pyramid Pattern.
Code:
import java.util.Scanner;
public class FibonacciHalfPyramidPattern {
public static void main(String[] args) {
Scanner cs = new Scanner(System.in);
System.out.println("Enter the row size:");
int out, in, a = 0, b = 1, c = a + b;
int row_size = cs.nextInt();
for (out = 1; out <= row_size; out++) {
for (in = 1; in <= out; in++) {
System.out.print(c + " ");
c = a + b;
a = b;
b = c;
}
System.out.println();
}
cs.close();
}
}
Enter the row size:
5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
Program in Python
Here is the source code of the Python Program to print the Fibonacci Half Pyramid Pattern.
Code:
row_size = int(input("Enter the row size: "))a = 0b = 1c = a+bfor out in range(1, row_size+1):for inn in range(1, out+1):print(c, end=" ")c = a+ba = bb = cprint("\r")
Enter the row size:3
1
1 2
3 5 8
1 Comments
0 is not printing at start
ReplyDeletePlease do not Enter any spam link in the comment box