Skip to content
Related Articles

Related Articles

Program to print Collatz Sequence
  • Difficulty Level : Easy
  • Last Updated : 19 Mar, 2021

Starting with any positive integer N, Collatz sequence is defined corresponding to n as the numbers formed by the following operations :
 

  1. If n is even, then n = n / 2.
  2. If n is odd, then n = 3*n + 1.
  3. Repeat above steps, until it becomes 1.

Examples : 
 

Input : 3
Output : 3, 10, 5, 16, 8, 4, 2, 1       

Input : 6
Output : 6, 3, 10, 5, 16, 8, 4, 2, 1

 

Below is the implementation : 
 

C++




// CPP program to print Collatz sequence
#include <bits/stdc++.h>
using namespace std;
 
void printCollatz(int n)
{
    // We simply follow steps
    // while we do not reach 1
    while (n != 1)
    {
        cout << n << " ";
 
        // If n is odd
        if (n & 1)
            n = 3*n + 1;
 
        // If even
        else
            n = n/2;
    }
 
    // Print 1 at the end
    cout << n;
}
 
// Driver code
int main()
{
    printCollatz(6);
    return 0;
}

Java




// Java program to print
// Collatz sequence
import java.io.*;
 
class GFG
{
    static void printCollatz(int n)
    {
        // We simply follow steps
        // while we do not reach 1
        while (n != 1)
        {
            System.out.print(n + " ");
     
            // If n is odd
            if ((n & 1) == 1)
                n = 3 * n + 1;
     
            // If even
            else
                n = n / 2;
        }
     
        // Print 1 at the end
        System.out.print(n);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        printCollatz(6);
    }
}
 
// This code is contributed
// by akt_mit

Python3




# Python 3 program to print
# Collatz sequence
 
def printCollatz(n):
     
    # We simply follow steps
    # while we do not reach 1
    while n != 1:
        print(n, end = ' ')
 
        # If n is odd
        if n & 1:
            n = 3 * n + 1
 
        # If even
        else:
            n = n // 2
 
    # Print 1 at the end
    print(n)
 
# Driver code
printCollatz(6)
 
# This code is contributed
# by vaibhav29498

C#




// C# program to print Collatz sequence
using System;
 
class GFG {
     
    static void printCollatz(int n)
    {
        // We simply follow steps
        // while we do not reach 1
        while (n != 1)
        {
            Console.Write (n + " ");
     
            // If n is odd
            if ((n & 1) == 1)
                n = 3 * n + 1;
     
            // If even
            else
                n = n / 2;
        }
     
        // Print 1 at the end
        Console.Write (n);
    }
     
    // Driver code
    static void Main()
    {
        printCollatz(6);
    }
}
 
// This code is contributed by
// Manish Shaw (manishshaw1)

PHP




<?php
// PHP program to print Collatz sequence
 
function printCollatz($n)
{
    // We simply follow steps
    // while we do not reach 1
    while ($n != 1)
    {
        echo $n . " ";
 
        // If $n is odd
        if ($n & 1)
            $n = 3 * $n + 1;
 
        // If even
        else
            $n = $n / 2;
    }
 
    // Print 1 at the end
    echo $n;
}
 
// Driver code
printCollatz(6);
 
// This code is contributed
// by ChitraNayal
?>

Javascript




<script>
 
    // Javascript program to print Collatz sequence
     
    function printCollatz(n)
    {
        // We simply follow steps
        // while we do not reach 1
        while (n != 1)
        {
            document.write(n + " ");
 
            // If n is odd
            if ((n & 1) != 0)
                n = 3*n + 1;
 
            // If even
            else
                n = parseInt(n/2, 10);
        }
 
        // Print 1 at the end
        document.write(n);
    }
 
    printCollatz(6);
 
 
</script>

Output 
 

6 3 10 5 16 8 4 2 1

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :