Pattern 12

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

1. You are given a number n.
2. You've to create a pattern as shown in output format.
Input Format
A number n
Output Format
pat121
Question Video
Constraints
1 <= n <= 5
Sample Input
5
Sample Output
0	
1 1
2 3 5
8 13 21 34
55 89 144 233 377


  • Editorial

    "Why do java programmers always wear glasses? Because they do not C#."

    Question:

    1. You are given a number n.

    2. You have to create a pattern as shown in the output format.

    Input format:

    A number n

    Output format:


    Constraints:

    1 <= n <= 5

    Solution Approach:

    We have to print a triangle of Fibonacci sequence numbers in the given problem. Now, we have already discussed how to print the Fibonacci series and triangle of stars on the way to this problem.

    Using that knowledge, we will move forward to combine them and implement it in our program.

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            int a = 0,b = 1; // First two Fibonacci numbers
                                    
                                            for(int i =1;i<=n;i++)
                                            {
                                                for(int j = 1;j<=i;j++)
                                                {
                                                    System.out.print(a + "	");
                                                    int c = a + b;  //Since next fibonacci number is sum of                    
                                                    a = b;         //previous two fibonacci numbers
                                                    b = c;
                                                }
                                                System.out.println();
                                            }
                                        }
                                    }

    java false

    Output for input value n = 5:

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name