Print Tribonacci Numbers

Here given code implementation process.

//C Program
//Tribonacci Numbers
#include <stdio.h>


void tribonacci(int n)
{
  //Set the initial value of variable
  //This is Three initial value
  int first  = 0;
  int second = 1;
  int third  = 1;
  
  int result = 0;

  //Base cases
  if(n > 0)
  {
    printf("  %d",0);
  }
  if(n > 1)
  {
    printf("  %d",0);
  }
  if(n > 2)
  {
    printf("  %d",1);
  }
  if(n > 3)
  {
    printf("  %d",1);
  }
  n-=4;
  
  while(n > 0)
  {

    result = first + second + third;

    printf("  %d",result);

    first  = second;

    second = third;

    third  = result;

    n--;
  } 
  
}

int main()
{
 
  int n = 15;
  
  tribonacci(n);

  return 0;
}

Output

0  0  1  1  2  4  7  13  24  44  81  149  274  504  927
/*
  C++ Program
  Tribonacci Numbers
*/

#include<iostream>

using namespace std;

class Number {
  public:
    void tribonacci(int n) {
      //Set the initial value of variable
      //This is Three initial value
      int first = 0;
      int second = 1;
      int third = 1;
      int result = 0;
      if (n > 0) {
        cout << ("  0");
      }
      if (n > 1) {
        cout << ("  0");
      }
      if (n > 2) {
        cout << ("  1");
      }
      if (n > 3) {
        cout << ("  1");
      }
      n -= 4;
      while (n > 0) {
        result = first + second + third;
        cout << "  " << result;
        first = second;
        second = third;
        third = result;
        n--;
      }
    }

};
int main() {
  Number obj;
  int n = 15;
  obj.tribonacci(n);
}

Output

  0  0  1  1  2  4  7  13  24  44  81  149  274  504  927
/*
  Java Program
  Tribonacci Numbers
*/
public class Number {

  public void tribonacci(int n) {
    //Set the initial value of variable
    //This is Three initial value
    int first = 0;
    int second = 1;
    int third = 1;

    int result = 0;

    //Base cases
    if (n > 0) {
      System.out.print("  0");
    }
    if (n > 1) {
      System.out.print("  0");
    }
    if (n > 2) {
      System.out.print("  1");
    }
    if (n > 3) {
      System.out.print("  1");
    }
    n -= 4;

    while (n > 0) {

      result = first + second + third;

      System.out.print("  " + result);

      first = second;

      second = third;

      third = result;

      n--;
    }

  }
  public static void main(String[] args) {
    Number obj = new Number();

    int n = 15;

    obj.tribonacci(n);

  }
}

Output

  0  0  1  1  2  4  7  13  24  44  81  149  274  504  927
#Python3 Program
#Tribonacci Numbers
class Number :
  def tribonacci(self, n) :

    #Set the initial value of variable
    #This is Three initial value
    first = 0
    second = 1
    third = 1
    result = 0
    if (n > 0) :
      print(0,end=" ")
    
    if (n > 1) :
      print(0,end=" ")
    
    if (n > 2) :
      print(1,end=" ")
    
    if (n > 3) :
      print(1,end=" ")
    
    n -= 4
    while (n > 0) :
      result = first + second + third
      print(result,end=" ")
      first = second
      second = third
      third = result
      n -= 1
    
  
def main() :
  obj = Number()
  n = 15
  obj.tribonacci(n)
  

if __name__ == "__main__":
  main()

Output

0 0 1 1 2 4 7 13 24 44 81 149 274 504 927
/*
  C# Program
  Tribonacci Numbers
*/
using System;
public class Number {

  public void tribonacci(int n)
  {
    //Set the initial value of variable
    //This is Three initial value
    int first  = 0;
    int second = 1;
    int third  = 1;

    int result = 0;

    //Base cases
    if(n > 0)
    {
      Console.Write("  0");
    }
    if(n > 1)
    {
      Console.Write("  0");
    }
    if(n > 2)
    {
      Console.Write("  1");
    }
    if(n > 3)
    {
      Console.Write("  1");
    }
    n-=4;

    while(n > 0)
    {

      result = first + second + third;

      Console.Write("  "+result);

      first  = second;

      second = third;

      third  = result;

      n--;
    } 

  }
  public static void Main(String[] args) {
    Number obj = new Number();

    int n = 15;

    obj.tribonacci(n);

  }
}

Output

0  0  1  1  2  4  7  13  24  44  81  149  274  504  927
#Ruby Program
#Tribonacci Numbers
class Number 
  def tribonacci(n) 
    first = 0
    second = 1
    third = 1
    result = 0
    if (n > 0) 
      print("  0")
    end
    if (n > 1) 
      print("  0")
    end
    if (n > 2) 
      print("  1")
    end
    if (n > 3) 
      print("  1")
    end
    n -= 4
    while (n > 0) 
      result = first + second + third
      print("  ", result)
      first = second
      second = third
      third = result
      n -= 1
    end
  end
end
def main() 
  obj = Number.new()
  n = 15
  obj.tribonacci(n)
end
main()

Output

 0  0  1  1  2  4  7  13  24  44  81  149  274  504  927
/*
  Node Js Program
  Tribonacci Numbers
*/
class Number {
  tribonacci(n) {
    var first = 0;
    var second = 1;
    var third = 1;
    var result = 0;
    if (n > 0) {
      process.stdout.write("  0");
    }
    if (n > 1) {
      process.stdout.write("  0");
    }
    if (n > 2) {
      process.stdout.write("  1");
    }
    if (n > 3) {
      process.stdout.write("  1");
    }
    n -= 4;
    while (n > 0) {
      result = first + second + third;
      process.stdout.write("  " + result);
      first = second;
      second = third;
      third = result;
      n--;
    }
  }
}

function main() {
  var obj = new Number();
  var n = 15;
  obj.tribonacci(n);
}
main();

Output

 0  0  1  1  2  4  7  13  24  44  81  149  274  504  927
/*
  Swift 4
  Tribonacci Numbers
*/
class Number {
  func tribonacci(_ n: Int) {
    var first: Int = 0;
    var second: Int = 1;
    var third: Int = 1;
    var result: Int = 0;
    var num = n;
    if (n > 0) {
      print(0,terminator : "  ");
    }
    if (n > 1) {
      print(0,terminator : "  ");
    }
    if (n > 2) {
      print(1,terminator : "  ");
    }
    if (n > 3) {
     
      print(1,terminator : "  ");
    }
    num = n - 4;
    while (num > 0) {
      result = first + second + third;
      print(result,terminator : "  ");
      first = second;
      second = third;
      third = result;
      num -= 1;
    }
  }

}
func main() {
  let obj: Number = Number();
  let n: Int = 15;
  obj.tribonacci(n);
}
main();

Output

 0  0  1  1  2  4  7  13  24  44  81  149  274  504  927
#   Python 3 Program
#   Tribonacci Numbers

class Number :
  def tribonacci(self, n) :
    # Set the initial value of variable
    # This is Three initial value
    first = 0
    second = 1
    third = 1
    result = 0
    # Base cases

    if (n > 0) :
      print(" 0", end = " ")
    
    if (n > 1) :
      print(" 0", end = " ")
    
    if (n > 2) :
      print(" 1", end = " ")
    
    if (n > 3) :
      print(" 1", end = "")
    
    n -= 4
    while (n > 0) :
      result = first + second + third
      print(" ", result, end = " ")
      first = second
      second = third
      third = result
      n -= 1
    
  

def main() :
  obj = Number()
  n = 15
  obj.tribonacci(n)


if __name__ == "__main__":
  main()

Output

 0  0  1  1  2   4   7   13   24   44   81   149   274   504   927
/*
  Scala Program
  Tribonacci Numbers
*/
class Number {
  def tribonacci(value: Int): Unit = {
    //Set the initial value of variable
    //This is Three initial value
    var first: Int = 0;
    var second: Int = 1;
    var third: Int = 1;
    var result: Int = 0;

    //Base cases

    if (value > 0) {
      print(" 0");
    }
    if (value > 1) {
      print(" 0");
    }
    if (value > 2) {
      print(" 1");
    }
    if (value > 3) {
      print(" 1");
    }
    var n : Int = value-4;
    while (n > 0) {
      result = first + second + third;
      print(" " + result);
      first = second;
      second = third;
      third = result;
      n -= 1;
    }
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: Number = new Number();
    var n: Int = 15;
    obj.tribonacci(n);
  }
}

Output

 0 0 1 1 2 4 7 13 24 44 81 149 274 504 927
fn main(){
  let n: i32 = 15;
  tribonacci(n);
}
fn tribonacci(n: i32) {
  //Set the initial value of variable
  //This is Three initial value
  let mut first: i32 = 0;
  let mut second: i32 = 1;
  let mut third: i32 = 1;
  let mut result: i32 ;
  //Base cases

  if n > 0 {
    print!(" {}", 0);
  }
  if n > 1 {
    print!(" {}", 0);
  }
  if n > 2 {
    print!(" {}", 1);
  }
  if n > 3 {
    print!(" {}", 1);
  }
  let mut value = n - 4;
  while value > 0 {
    result = first + second + third;
    print!(" {}", result);
    first = second;
    second = third;
    third = result;
    value-= 1;
  }
}

Output

 0 0 1 1 2 4 7 13 24 44 81 149 274 504 927


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved