// Define the recursive function.
int collatz(int p1)
{
// While Loop Starting
while (p1>1)
{
//when the number is even
if(p1%2==0)
{
p1 = p1/2;
printf("%d ", p1);
//using recursion
return collatz(p1);
}
// Case where number is odd.
elseif
{
p1 = 3*p1+1;
//print function
printf("%d ", p1);
//using recursion
return collatz(p1);
}
}
}
// Main body.
int main()
{
// Declare the variable and initialized it.
int p1= 4;
//print function
printf("User Entered value : %d\n", p1);
// Display the number
printf("%d\n", collatz(p1));
//End
return 0;
}
输出:我得到的输出为:2 ,1, 1 我不应该重复最后一个数字 1。你能纠正我做错的地方吗?请做必要的事情。