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.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
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.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
Solution
Here is a Python program that achieves the described functionality:
def calculate(n):
if n > 0:
return "Cube value: " + str(n**3)
else:
return "Squared value: " + str(n**2)
# Test the function
print(calculate(100)) # Cube value: 1000000
print(calculate(-20)) # Squared value: 400
print(calculate(0)) # Squared value: 0
This program defines a function calculate(n) that takes an integer n as input. If n is positive, it calculates the cube of n and returns a string in the format "Cube value: " followed by the cube value. If n is negative or zero, it calculates the square of n and returns a string in the format "Squared value: " followed by the square value. The function is then tested with the sample inputs.
Similar Questions
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.
Problem StatementImagine you are tasked with creating a program that calculates the cube of a given number. Your goal is to write a program that takes an integer n from the user, calculates its cube using a call-by-reference function, and then prints the result.Function Specifications: int cube(int *n) Input format :The input consists of a positive integer n.Output format :The output prints an integer which is the cube value of n.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 103Sample test cases :Input 1 :1Output 1 :1Input 2 :5Output 2 :125Input 3 :1000Output 3 :1000000000
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.
Write a C program to find cube of any number using function.
Imagine you are tasked with creating a program that calculates the cube of a given number. Your goal is to write a program that takes an integer n from the user, calculates its cube using a call-by-reference function, and then prints the result.Function Specifications: int cube(int *n) Input format :The input consists of a positive integer n.Output format :The output prints an integer which is the cube value of n.Refer to the sample output for formatting specifications.Code constraints :
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.