Read a integer number N.Print the cube of N if N is even.Print the square of N if N is odd.Implement the scenario using the switch case statement.Input4OutputEven64
Question
Read a integer number N.Print the cube of N if N is even.Print the square of N if N is odd.Implement the scenario using the switch case statement.Input4OutputEven64
Solution
The problem you've described can be solved using a programming language. However, it's important to note that the switch case statement is not typically used to check if a number is even or odd. Instead, it's used to allow a variable to be tested for equality against a list of values.
Here's how you might solve this problem in Java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
int N = scanner.nextInt();
switch (N % 2) {
case 0:
System.out.println("Even " + (N * N * N));
break;
case 1:
System.out.println("Odd " + (N * N));
break;
}
}
}
In this code:
- We first import the Scanner class, which is used to get user input.
- We then create a new instance of the Scanner class.
- We ask the user to enter a number and store this number in the variable
N. - We use a switch statement to check if
Nis even or odd by checking the remainder ofNdivided by 2 (N % 2). - If
Nis even (N % 2is 0), we print "Even" followed by the cube ofN(N * N * N). - If
Nis odd (N % 2is 1), we print "Odd" followed by the square ofN(N * N). - The
breakkeyword is used to exit the switch statement after a case has been matched and executed.
Similar Questions
Write an HTML and JavaScript program which accepts N as input and print N is oddor even number
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.
Write code that allow the user to input a number. Use for loop to print the number and 4 numbers after this number the user. For example, if the input is 5, then the program should print:56789
Problem StatementWrite a program to print the Square and Cube of the numbers from 1 to nInput FormatA single integer - nOutput FormatDisplay i i2 i3 space separated where i = 1 to n in new line.Sample Input5Sample Output1 1 12 4 83 9 274 16 645 25 125
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
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.