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