Hi!
I wrote a program on iteration of numbers means--
if n=10 ---n=10/2=5 5 is odd so 3(n)+1 upto getting 1.
Here is program

#include<stdio.h>
main()
{
int i,n,c=0;

printf("n= ");


scanf("%d",n);
while(n!=1){
if(n%2==0){
	n=n/2;
           }
else{
n=3*n+1;

	}
printf("%d\t",n);
c++;



	
printf("%d\n",c);/*printing no of iterations of n*/
}
}

I wanted to change this program so as to do this upto a number 10 or x
I have tried using for loop but it doesn't work

for(n=1;n<=10;++n)

Please help me writing it .Any help would be appreciated..Thanks
Sorry If my post is posted bad..
If you don't undrstand please see this question

Start with a positive integer n. If n is even, divide it by 2, else replace n by 3n+1. Repeat until we obtain the value 1 in the sequence. The 3x+1 conjecture says that we eventually reach 1 irrespective of what initial value of n we start with.

Different initial values of n lead to different numbers of iterations of the loop before hitting the value 1. For example, if we start with n=3, we get a sequence 3,10,5,16,8,4,2,1 using 7 iterations. If we start with 7, we need 16 iterations, since the sequence is now 7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1.

You are given a bound B. Your task is to locate that particular value of n in the range 1<=n<=B for which the number of iterations is maximum. Once this integer nis located, you print n, the corresponding number of iterations, and finally the sequence associated with n.

If the bound is 10, then the output of your program would look like:

Maximum number of iterations = 19 for n = 9.

The sequence is:

9 28 14 7 22 11 34 17

52 26 13 40 20 10 5 16

8 4 2 1

Report the output of your program for the bound 100,000.

Recommended Answers

All 3 Replies

something like this

void compute(int n)
{
    int iterations = 0;
    printf("n = %d -- ", n);
    while( n > 1)
    {
        if( (n%2) == 0)
        {
            n /= 2;
        }
        else
        {
            n = (3*n)+1;

        }
        printf("%d ", n);

        ++iterations;
    }
    printf("\niterations = %d\n",iterations);

}

int main()
{
    for(int n = 1; n <= 10; n++)
        compute(n);
}

Oh! I have now got some thing and it is working...
But I want to return the maximum count of all numbers let us suppose if
iteration count of 9 is 19 and 10 is 6 so we have to return 19 as maximum please help me.Thanx

#include<stdio.h>
int sign(int n)
{
int c=0,t=1;
while(n!=1){
	if(n%2==0){
	n=n/2;
           }
	else{
	n=3*n+1;
		}

	c++;
	}

printf("%d\n",c);
return c;
if(t>c){

	t=n;
	printf("mit= %d",t);
	}
}
main()
{
int i;
int c;
for(i=1;i<=10;++i){
	sign(i);
	
		}

}

lines 18-22 will never get executed because of the return statement on line 17.

In main() keep track of the largest value that is return by sign() function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.