Knowee
Questions
Features
Study Tools

int crypto(int num) {    if (num % 2 == 0)        return num / 2;    else if (num % 2 != 0)        return num - 1;}int currency(int num) {    int bit = 0;    while (num > 0) {        num = crypto(num)        bit += 1;    }    return bit;}

Question

int crypto(int num) {    if (num % 2 == 0)        return num / 2;    else if (num % 2 != 0)        return num - 1;}int currency(int num) {    int bit = 0;    while (num > 0) {        num = crypto(num)        bit += 1;    }    return bit;}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The provided code is written in C or C++ language. It consists of two functions: crypto and currency.

The crypto function takes an integer num as an input. If num is even, it returns num / 2. If num is odd, it returns num - 1.

The currency function also takes an integer num as an input. It initializes a variable bit to 0. Then it enters a while loop which continues as long as num is greater than 0. Inside the loop, num is updated by calling the crypto function with num as the argument, and bit is incremented by 1. Finally, the function returns bit.

However, there is a syntax error in the currency function. After calling the crypto function, there should be a semicolon (;). The corrected code is:

int crypto(int num) {
    if (num % 2 == 0)
        return num / 2;
    else if (num % 2 != 0)
        return num - 1;
}

int currency(int num) {
    int bit = 0;
    while (num > 0) {
        num = crypto(num);
        bit += 1;
    }
    return bit;
}

This corrected currency function will now correctly calculate and return the number of times the crypto function needs to be applied to num to reduce it to 0.

This problem has been solved

Similar Questions

Predict the output for the following code for currency(213)int crypto(int num) {    if (num % 2 == 0)        return num / 2;    else if (num % 2 != 0)        return num - 1;}int currency(int num) {    int bit = 0;    while (num > 0) {        num = crypto(num)        bit += 1;    }    return bit;}

int  func( int num) {      int a = num, r =0;      while (num > 0) {        r = r * 10 + num % 10;            num /= 10;      }     return r;                  }What will be the output for func(123) ?

What will be the output of the following code?int num = 2;int count=0;while(num!= 0){if ( num & 01)count ++;num >>=1;}cout<< count;return 0;

Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in reverse binary. For an integer x, the algorithm is:As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.

Daniel is working on a program for mathematical operations. Create a program that takes two floating-point numbers, multiplies them, converts the result to an integer, and based on the user's choice:If the choice is 1, add 5 to the converted integer.If the choice is 2, subtract 5 from the converted integer.If the choice is 3, divide the converted integer by 2.Display the original multiplication result as a float with two decimal places, and then show the modified integer after the chosen operation. Input format :The first line of input consists of two float values: num1 and num2 separated by a space.The second line of input consists of an integer value n, representing Daniels's choice:If the choice is 1, add 5 to the converted integer.If the choice is 2, subtract 5 from the converted integer.If the choice is 3, divide the converted integer by 2.Output format :The first line displays "Multiplication Result (as float): " followed by the result of multiplying num1 and num2 as a float value with two decimal values.The second line displays "Converted Integer: " followed by the result as an integer value after performing the required operation on the converted result based on the choice.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.