A multiplication problem is provided as a string using the f

A multiplication problem is provided as a string using the format \'x times y\'. Extract the multiplicand x and multiplier y from inputString, and then assign multResult with the product. Ex: If inputString is \'4 times 3\', then multResult is 4 * 3, or 12.

Solution

Multiplication.java

import java.util.Scanner;

public class Multiplication {


   public static void main(String[] args) {
       //Declaring variables
       int x,y,multResult;
       String input;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the input string entered by the user
       System.out.print(\"Enter input :\");
       input=sc.nextLine();
      
       //Getting the value of x from the input string
       x=Integer.parseInt(input.substring(0,input.indexOf(\' \')));
      
       //Getting the value of y from the input string
       y=Integer.parseInt(input.substring(input.lastIndexOf(\' \')+1,input.length()));
      
       //Performing multiplication and stored into the variable multiResult
       multResult=x*y;
      
       //Displaying the output
       System.out.println(x+ \" * \"+y+\" is \"+multResult);
      
   }

}

________________

Output:

Enter input :4 times 3
4 * 3 is 12

__________Thank You

 A multiplication problem is provided as a string using the format \'x times y\'. Extract the multiplicand x and multiplier y from inputString, and then assign

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site