-1
#include <cs50.h>
#include <stdio.h>

int main(void)
{
int n = get_int("Enter An Integer: ");
do

{

    if (n==1)
    {
        printf("Done");
    }
    else if (n%2==0)
    {
        n=n/2;
        printf("%i", n);
    }
    else if(n%2!=0)
    {
        n= 3*n + 1;
        printf("%i",n);

    }

    else
        return 0;
}

while (n!=1);

}

2
  • 2
    "...not executing properly" What do you mean? Can you provide examples? What's not working correctly? How can the problem be replicated? Please provide details.
    – Cliff B
    Jun 24 '18 at 21:18
  • @CliffB When i pass in input as 4 it gave me output as 21 but i want the output to be similar to collatz function Jun 25 '18 at 8:48
1

Your program is printing as expected, in terms that it should print the value of n after every iteration, however you do have some errors in formatting your printf statement. Specifically, you forgot to add in a newline after printing the number itself.

Here's a fixed version of your code:

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    int n = get_int("Enter An Integer: ");

    do
    {
        if (n == 1)
        {
            printf("Done");
        }

        // Don't forget your '\n'!
        else if (n % 2 == 0)
        {
            n = n / 2;
            printf("%i\n", n);
        }

        // Same as n % 2 != 0, just some syntactic sugar!
        else if (n % 2)
        {
            n= 3 * n + 1;
            printf("%i\n",n);
        }

    } while (n != 1);

    // No need to return 0 within the do loop!
    return 0;
}

Not the answer you're looking for? Browse other questions tagged .