patternprogram_57 softethics

Pattern 57 (Fibonacci Series)

C

#include<stdio.h>

int main()
{
  int i,j;
  int n1=0,n2=1,r=n1+n2;


  for(i=1; i<=5; i++)
  {
    for(j=1; j<=i; j++)
    {

      printf("%d ",r);
      r=n1+n2;
      n1=n2;
      n2=r;
    }
    printf("\n");
  }

}

C++

 #include<iostream.h>
 
 int main()
 {
   int i,j;
   int n1=0,n2=1,r=n1+n2;
 
 
  for(int i =1;i<=5;i++)
  {
   for(int j =1;j<=i;j++)
   {
   cout<<r<<" ";
   r=n1+n2;
   n1=n2;
   n2=r;
   }
   cout<<endl;
  }
 
 }

Java

     public class PatternProg
 
     public static void main(String agrs[]) {
         int i;
         int j;
         int n1 = 0;
         int n2 = 1;
         int r = n1 + n2;
 
 
         for (i = 1; i <= 5; i++) {
             for (j = 1; j <= i; j++) {
 
                 System.out.printf("%d ", r);
                 r = n1 + n2;
                 n1 = n2;
                 n2 = r;
             }
             System.out.print("\n");
         }
 
     }
 }

C#

using System;

public class PatternProg
{

  public static void Main(string[] agrs)
  {
    int i;
    int j;
    int n1 = 0;
    int n2 = 1;
    int r = n1 + n2;


    for (i = 1; i <= 5; i++)
    {
      for (j = 1; j <= i; j++)
      {

        Console.Write("{0:D} ", r);
        r = n1 + n2;
        n1 = n2;
        n2 = r;
      }
      Console.WriteLine();
    }
    Console.ReadKey(true);
  }
}

Python

n = 5  # size
n1 = 0
n2 = 1
r = n1 + n2

for x in range(0, n):
    for y in range(0, x + 1):
        print(str(r) + " ", end="")
        r = n1 + n2
        n1 = n2
        n2 = r
    print()
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments