Ask Question

Write a while loop that prints user_num divided by 2 until user_num is less than 1. The value of user_num changes inside of the loop. Sample output with input: 20 10.0 5.0 2.5 1.25 0.625

+1
Answers (1)
  1. 11 April, 09:01
    0
    The code to this question can be defined as follows:

    Code:

    #include / /defining header file

    int main () / /defining main method

    {

    float user_num; / /defining float variable

    printf ("Enter a value: "); / /message

    scanf ("%f",&user_num); / /input value from user end

    printf ("%f, ", user_num); / /print value

    while (user_num>1) / /loop to calculte value

    {

    user_num=user_num/2; / /diving value

    printf ("%f, ", user_num); / /print value.

    }

    return 0;

    }

    Output:

    Enter a value: 20

    20.000000, 10.000000, 5.000000, 2.500000, 1.250000, 0.625000,

    Explanation:

    Description of the code as follows:

    First, a float variable "user_num" is declared, in which we take input from the user-end. In the next step, a while loop is declared, that uses the above variable to calculates its value. Inside the loop variable "user_num" divide its value by 2 and holds its calculated value, to print its value the "printf" method is used that prints its value.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a while loop that prints user_num divided by 2 until user_num is less than 1. The value of user_num changes inside of the loop. ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers