Write a C program that asks for an integer n and then comput
Solution
#include <stdio.h>
void main()
{
//declare variables
int n,cnt=0;
//prompt user for n
printf(\"Enter value of n: \");
scanf(\"%d\",&n);
//check if n>1
if(n>1)
{
do{
//check if n is even
if(n%2==0)
{
//if yes,increment count and divide by 2
cnt++;
n=n/2;
printf(\"%d\\t\",n);
}
//check if n is odd
else
{
cnt++;
n=3*n+1;
printf(\"%d\\t\",n);
}
//continue this until n<=1
}while(n>1);
}
//print no of sequence
printf(\"\ Count: %d\",cnt);
}
Sample Output:
Enter value of n: 10
5 16 8 4 2 1
Count: 6
