## Program to Print Collatz Conjecture for a Given Number in C++ and Python

In the previous article, we have discussed about Program to Read a Number n and Compute n+nn+nnn in C++ and Python. Let us learn Program to Print Collatz Conjecture for a Given Number in C++ Program.

Given a number , the task is to print Collatz Conjecture of the given number in C++ and Python.

**Collatz Conjecture:**

- The Collatz Conjecture states that a specific sequence will always reach the value 1.
- It is given as follows, beginning with some integer n:
- If n is an even number, the following number in the sequence is n/2.
- Otherwise, the following number is 3n+1 (if n is odd).

**Examples:**

**Example1:**

**Input:**

given number =5425

**Output:**

The Collatz Conjecture of the number : 5425 16276 8138 4069 12208 6104 3052 1526 763 2290 1145 3436 1718 859 2578 1289 3868 1934 967 2902 1451 4354 2177 6532 3266 1633 4900 2450 1225 3676 1838 919 2758 1379 4138 2069 6208 3104 1552 776 388 194 97 292 146 73 220 110 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1

**Example2:**

**Input:**

given number=847

**Output:**

The Collatz Conjecture of the number : 847 2542 1271 3814 1907 5722 2861 8584 4292 2146 1073 3220 1610 805 2416 1208 604 302 151 454 227 682 341 1024 512 256 128 64 32 16 8 4 2 1

## Program to Print Collatz Conjecture for a Given Number in C++ and Python

- Printing Collatz Conjecture sequence of the given number in Python
- Printing Collatz Conjecture sequence of the given number in C++

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

### 1)Printing Collatz Conjecture sequence of the given number in Python

**Approach:**

- Scan the given number or give number as static input.
- Iterate till the given number is not equal to 1 using while loop.
- Print the number
**numb**. - If the number is even then set n to n/2.
- If the number is odd then set n to 3*n+1.
- Print 1 after end of while loop.
- The Exit of the Program.

**Below is the implementation:**

# function which prints collatz sequence of the given number def printCollatz(numb): # Iterate till the given number is not equal to 1 using while loop. while numb > 1: # Print the number numb print(numb, end=' ') # If the number is even then set n to n/2. if (numb % 2 == 0): numb = numb//2 # If the number is odd then set n to 3*n+1. else: numb = 3*numb + 1 # Print 1 after end of while loop. print(1, end='') # given number numb = 179 print('The Collatz Conjecture of the number :') # passing the given numb to printCollatz function to # print collatzConjecture sequence of the given number printCollatz(numb)

**Output:**

The Collatz Conjecture of the number : 179 538 269 808 404 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

### 2)Printing Collatz Conjecture sequence of the given number in C++

**Approach:**

- Scan the given number using cin or give number as static input
- Iterate till the given number is not equal to 1 using while loop.
- Print the number
**numb**. - If the number is even then set n to n/2.
- If the number is odd then set n to 3*n+1.
- Print 1 after end of while loop.

**Below is the implementation:**

#include <bits/stdc++.h> using namespace std; // function which prints collatz sequence of the given // number void printCollatz(int numb) { // Iterate till the given number is not equal to 1 using // while loop. while (numb > 1) { // Print the number numb cout << numb << " "; // the number is even then set n to n / 2. if (numb % 2 == 0) { numb = numb / 2; } // the number is odd then set n to 3 * n + 1. else { numb = 3 * numb + 1; } } // Print 1 after end of while loop. cout << " 1"; } int main() { // given number int numb = 179; cout << "The Collatz Conjecture of the number :" << endl; // passing the given numb to printCollatz function to // print collatzConjecture sequence of the given number printCollatz(numb); return 0; }

**Output:**

The Collatz Conjecture of the number : 179 538 269 808 404 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

**Related Programs**:

- Python Program to Print Output Without a Newline
- Python Program to Print the Natural Numbers Summation Pattern
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- Python Program to Print Binary Representation of a Number
- Python Program to Check if a Date is Valid and Print the Incremented Date