Collatz Conjucture in c++ || C++
In this, we are going see a program to find Collatz Conjecture in C++ Programming Language.
 


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <stdio.h>
#include <conio.h>

void main()
{
    int x;
    cout << "Enter a number: ";
    cin >> x;
    cout << "The series of numbers are: \n";
    while (x != 1)
    {
        cout << x << " ";
        if (x % 2 == 1)
        {
            x = x * 3 + 1;
        }
        else
        {
            x = x / 2;
        }
    }
    cout << "1";
    getch();
    clrscr();
}

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;
int main()
{
    int x;
    cout << "Enter a number: ";
    cin >> x;
    cout << "The series of numbers are: \n";
    while (x != 1)
    {
        cout << x << " ";
        if (x % 2 == 1)
            x = x * 3 + 1;
        else
            x = x / 2;
    }
    cout << "1";
    return 0;
}

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post