Computer Science, asked by Anonymous, 5 hours ago

wap to accept a 4-digit no. and print the product of the 1st two & last two digits using the method ---- public void multi ( int num)

if u know then only answer otherwise don't
or else ur answer will be reported ....

Answers

Answered by ranjana1980sri
6

Answer:

We are assuming that the user will enter a proper 4 digit integer.

We can take this user input with the help of Scanner.

After this, we need to extract the digits.

If we take modulo 10, i.e. number % 10, we will get the units digit as remainder.

We can store this. Now, if we divide by 10, i.e. number / 10, we will get the quotient of the division. Essentially, we would get the number with the units digit removed.

We can now again use the % 10 trick to extract the new units digit.

In this way we can get the digits of a number from right to left. We can then find the sum of the first two digits and product of the last two digits.

\rule{300}{1}

import java.util.Scanner; //Importing Scanner

public class DigitOperations //Creating Class

{

public static void main(String[] args) //Main Function

{

//Create Scanner Object

Scanner scr = new Scanner(System.in);

//Take User Input

System.out.print("Enter a four digit number: ");

int num = scr.nextInt(); //Storing Input in num

int digit1 = num % 10; //Extracting Unit's Digit

num /= 10; //Dividing by 10 to remove unit's digit

int digit2 = num%10; //Extracting Ten's Digit

num /= 10; //Dividing by 10 to remove original ten's digit

int digit3 = num%10; //Extracting Hundred's Digit

num /= 10; //Dividing by 10 to remove original hundred's digit

int digit4 = num%10; //Extracting Thousand's Digit

//Now, we have extracted the digits from right to left

//So, digit1 and digit2 are the last two two digits

//digit3 and digit4 are the first two digits

int sum = digit3 + digit4;

int product = digit1 * digit2;

System.out.println("Sum of first two digits = "+sum);

System.out.println("Product of last two digits = "+product);

Similar questions