Knowee
Questions
Features
Study Tools

Kevin is trying to solve a number puzzle. If the number is greater than fifty, he wants the difference between the digits of the number; otherwise, if the number is less than or equal to fifty, he wants to reverse the number and find the difference in digits. Help Kevin to solve the problem using a Java program.Requirements:The input number should be a two-digit integer, or else display "Invalid number".Note:In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input/Output 1:Enter the number56-1Explanation:56 is greater than 50, so subtract 5 - 6 = -1Sample Input/Output 2:Enter the number484Explanation:48 is less than 50, so reverse 48 to 84 then subtract 8 - 4 = 4Sample Input/Output 3:Enter the number9Invalid number

Question

Kevin is trying to solve a number puzzle. If the number is greater than fifty, he wants the difference between the digits of the number; otherwise, if the number is less than or equal to fifty, he wants to reverse the number and find the difference in digits. Help Kevin to solve the problem using a Java program.Requirements:The input number should be a two-digit integer, or else display "Invalid number".Note:In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input/Output 1:Enter the number56-1Explanation:56 is greater than 50, so subtract 5 - 6 = -1Sample Input/Output 2:Enter the number484Explanation:48 is less than 50, so reverse 48 to 84 then subtract 8 - 4 = 4Sample Input/Output 3:Enter the number9Invalid number

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Java program that meets the requirements:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number");
        String input = scanner.nextLine();
        if (input.length() != 2) {
            System.out.println("Invalid number");
        } else {
            int number = Integer.parseInt(input);
            if (number > 50) {
                System.out.println(Character.getNumericValue(input.charAt(0)) - Character.getNumericValue(input.charAt(1)));
            } else {
                System.out.println(Character.getNumericValue(input.charAt(1)) - Character.getNumericValue(input.charAt(0)));
            }
        }
    }
}

This program first checks if the input number is a two-digit number. If not, it prints "Invalid number". If it is a two-digit number, it checks if the number is greater than 50. If it is, it subtracts the second digit from the first. If it's not, it subtracts the first digit from the second (effectively reversing the number and finding the difference in digits).

This problem has been solved

Similar Questions

public class Hello{ public static void main(String args[]) { int num = 190; String rev_num = " " ; while (num!=0){ int digit = num%10; rev_num = rev_num+digit; num = num /= 10 ; } System.out.println((Integer.parseInt(rev_num)));} } Exception in thread "main" java.lang.NumberFormatException: For input string: " 091" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:654) at java.base/java.lang.Integer.parseInt(Integer.java:786)

Write a  program to find whether a given integer is positive or negative.Sample Input 1:Enter the number:45Sample Output 1:45 is a positive numberSample Input 2: Enter the number:-364Sample Output 2:-364 is a negative numberScanner sc = new Scanner(System.in);public static void main(String[] args) {import java.util.*;public class Main {if(num>=0)System.out.println(num+" is a negative number");elseSystem.out.println("Enter the number:");int num=sc.nextInt();     }}System.out.println(num+" is a positive number");

Thara is attending a math lecture, and her professor asks students to swap two numbers using a bitwise operator. The professor finds it difficult to check the logic of 50 students. So, he decided to write a Java program to get two numbers, first and second, from the user and swap the numbers. He is not good at programming. Help them by developing an application using your Java skills.Note:Use bitwise operator.Negative and zero are also accepted as inputs and numbers have to be swapped accordingly.In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input 1:Enter the numbers8743Sample Output 1:Before swappingfirst = 87, second = 43After swappingfirst = 43, second = 87

Java program that reads an integer between 0 and 1000 and adds all the digits in the integerWrite a Java program that reads an integer between 0 and 1000 and adds all the digits in the integer.An integer is a number that can be written without a fractional component. For example, 23, 6, 0, and −1245 are integers, while 3.25, ​7 1⁄2, and √3 are not. Click on the image for preview.Constraints:N/AExample:Inputan integer between 0 and 1000: 565Output:The sum of all digits in 565 is 16Explanation:-Public Test Cases:# INPUT EXPECTED OUTPUT1 56516

Create a program in Java that will accept two integers. If the sum of the integers is odd, display the higher integer, else, display the lower integer.

1/3

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.