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;}Select one:a. 12b. 6c. 100111d. True
Question
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;}Select one:a. 12b. 6c. 100111d. True
Solution
The function crypto takes an integer as input. If the number is even, it divides it by 2. If the number is odd, it subtracts 1.
The function currency also takes an integer as input. It initializes a variable bit to 0. Then, it enters a while loop that continues as long as num is greater than 0. In each iteration of the loop, num is updated to the result of crypto(num), and bit is incremented by 1. Finally, bit is returned.
So, if we call currency(213), the following will happen:
numis 213, which is odd, socrypto(213)returns 212.bitis incremented to 1.numis now 212, which is even, socrypto(212)returns 106.bitis incremented to 2.numis now 106, which is even, socrypto(106)returns 53.bitis incremented to 3.numis now 53, which is odd, socrypto(53)returns 52.bitis incremented to 4.numis now 52, which is even, socrypto(52)returns 26.bitis incremented to 5.numis now 26, which is even, socrypto(26)returns 13.bitis incremented to 6.numis now 13, which is odd, socrypto(13)returns 12.bitis incremented to 7.numis now 12, which is even, socrypto(12)returns 6.bitis incremented to 8.numis now 6, which is even, socrypto(6)returns 3.bitis incremented to 9.numis now 3, which is odd, socrypto(3)returns 2.bitis incremented to 10.numis now 2, which is even, socrypto(2)returns 1.bitis incremented to 11.numis now 1, which is odd, socrypto(1)returns 0.bitis incremented to 12.numis now 0, which is not greater than 0, so the loop ends.
Therefore, currency(213) returns 12. So, the correct answer is a. 12.
Similar Questions
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;}
Predict the output - int i=0; for (;i<5;){ i++; System.out.print(i); } A. 1234 B. 12345 C. 01234 D. 13
Predict the output of the following C Programming Snippet:#include <stdio.h>int main() { int a = 8, b = 6; printf("%d %d", a & b, a | b); return 0;}
Problem StatementA person is planning a trip abroad and wants to convert their local currency to the destination currency. Write a program for this where the exchange rate is fixed at 1.18. The program should take an amount in one currency as input, convert it to another currency using the exchange rate, and then output the converted amount rounded to two decimal places. Input format :The input consists of double value n, representing the currency.Output format :The output prints the converted currency in double value with two decimal places.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the given test cases fall under the following constraints:1.00 ≤ n ≤ 5000.00Sample test cases :Input 1 :1.00Output 1 :1.18Input 2 :5000.00Output 2 :5900.00Input 3 :2568.95Output 3 :3031.36
Predict the output of the following code snippetint func(int arr[], int size) { float val = 0; int i ; for(i = 0; i < size; i++) val += arr[i]; val /= size; if (val % 1 == 0) return 1; else return 0; }What will be the output for func(arr, 4) where arr = {2, 4, 8, 10 } ?
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.