Using the provided recording and your knowledge from programming 1. Create a program in java that takes in an input from the user and checks whether it’s a number or a word. In-case a number is entered the program should print the number to the power 3 and if a word is entered your program should split that word into 2. Sample Run1 Enter a number or word: 7 Output1: 7 to the power 3 = 343 Sample Run2 Enter a number or word: NAMIBIA Output2: The word split in half is : NAMI and BIA
package com.task;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number or word:");
String input = sc.next();
try {
Integer inputInt = Integer.valueOf(input);
System.out.println(inputInt + " to the power 3 = ");
System.out.println(inputInt*3);
} catch (NumberFormatException e) {
int halfLength = input.length()/2;
String first = input.substring(0, halfLength);
String second = input.substring(halfLength);
System.out.println("The word split in half is: " + first + " and " + second);
}
}
}
Comments
Leave a comment