samedi 28 mai 2016

Collatz sequence getting last number 1 repeated

    // 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;
    }

Output: I am getting the Output as : 2 ,1, 1 I should not get the last number 1 repeated.Could you please correct me where i have done mistake.Please do the needful.

Aucun commentaire:

Enregistrer un commentaire