Search This Blog

Saturday, 1 January 2022

3n+1 problem solving program in CPP

  3n+1 problem solving program in CPP

#include <iostream>

#include<conio.h>

using namespace std;


int cycle(long int n)//LOW 

{

    long int i=1;

    while(n!=1)

    {

        if(n%2 == 0)

        {

            n=n/2;

        }

        else

        {

            n=(3*n)+1;

        }

        i++;

    }

    return i;

}


int main()

{

    int a,b,low,high,count;

    std::cout << "Enter the series of numbes:" << std::endl;

    while(std::cin >> a>>b)

    {

        if(a<b)

        {

            low=a;

            high=b;

        }

        else

        {

            low=b;

            high=a;

        }

        int max = cycle(low);

        for(int i=low+1;i<=high;i++)

        {

            int l=cycle(i);

            if(l>max)

            {

                max=l;

            }

        }

        cout<<a<<" \t "<<b<<" \t "<<max<<"\n";

}

}

    

output:









No comments:

Post a Comment

Search This Blog

Contact Form

Name

Email *

Message *

Popular Posts