Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C programming question

  1. Aug 29, 2011 #1
    1. The problem statement, all variables and given/known data

    Simulate the following program

    #include <stdio.h>

    int xxx (int n)
    {
    int count;

    count = 0;

    while ( n >=2)
    {
    n = n/2;
    ++count;
    }

    return count;
    }

    main()
    {
    printf( "xxx(17)=%d\n", xxx(17) );
    }
    2. Relevant equations



    3. The attempt at a solution

    Is this correct?

    n, count
    17 0
    8 1
    4 2
    2 3
    1 4

    Return 4, 4 gets printed
     
  2. jcsd
  3. Aug 29, 2011 #2

    lewando

    User Avatar
    Homework Helper
    Gold Member

    Looks right.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook