Krish, a curious explorer in a unique computational environment, explores numerical transformations. The system swiftly calculates and displays the cube for positive numbers, while it diligently calculates and returns the square value for negative numbers.Write a program to achieve this and use the return statement accordingly.
Question
Krish, a curious explorer in a unique computational environment, explores numerical transformations. The system swiftly calculates and displays the cube for positive numbers, while it diligently calculates and returns the square value for negative numbers.Write a program to achieve this and use the return statement accordingly.
Solution
Sure, here is a Python program that achieves this:
def transform(num):
if num > 0:
return num ** 3
elif num < 0:
return num ** 2
else:
return 0
# Test the function
print(transform(5)) # Should print 125 (cube of 5)
print(transform(-4)) # Should print 16 (square of -4)
print(transform(0)) # Should print 0
In this program, the function transform takes a number as input. If the number is positive, it returns the cube of the number. If the number is negative, it returns the square of the number. If the number is zero, it simply returns zero.
Similar Questions
Problem StatementKrish, a curious explorer in a unique computational environment, explores numerical transformations. The system swiftly calculates and displays the cube for positive numbers, while it diligently calculates and returns the square value for negative numbers.Write a program to achieve this and use the return statement accordingly.Input format :The input consists of a single integer n.Output format :The output displays "Cube value: " followed by the cube value of n if the number is positive and terminates.The output displays "Squared value: " followed by the squared value of n if the number is negative or zero and terminates.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:-100 ≤ n ≤ 100Sample test cases :Input 1 :100Output 1 :Cube value: 1000000Input 2 :-20Output 2 :Squared value: 400Input 3 :0Output 3 :Squared value: 0
Write a C program to find cube of any number using function.
Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.You must not use any built-in exponent function or operator.For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. Example 1:Input: x = 4Output: 2Explanation: The square root of 4 is 2, so we return 2.Example 2:Input: x = 8Output: 2Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
Continuously input positive integers until 0 is entered. For each input, calculate and display its square and cube in a concise format, maintaining this process until the input is 0.Note: This question helps in clearing technical coding tests in companies like Wipro, TCS, Infosys.Input format :The input consists of positive integers. Enter 0 to terminate the input.
Study the following program:i = 0while i < 3: print(i) i += 1 else: print(0)
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.