// 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 repeated.Could請你糾正我在哪裏,我已經做了錯誤。請只做那些需要的。在Collatz序列獲得最後一個數字1重複
1)C和C++是不同的語言。選一個! 2)這既不是C也不是C++。 3)見[問]。 – Olaf
我寫過C – chandu
那麼爲什麼要爲其他語言添加標籤?請編輯您的問題,尊重其他兩個問題。 – Olaf